java - 如何在java中获取第三个斜杠之后的字符串

标签 java substring indexof

我正在尝试获取第三个斜杠之后的字符串。但我不知道该怎么做。我使用过 split,但这不是我想要的。

for(String obj2: listKey.getCommonPrefixes()){
    Map<String, String> map = new HashMap<String, String>();
    String[] id = obj2.split("/");
    if (id.length > 3) {
        String name = id[3];
        map.put("id", name);
        map.put("date", "null");
        map.put("size", String.valueOf(obj2.length()));
        keys.add(map);
    }
}

id[3] 只给我 id[3] 但我想要第三个斜杠之后的所有内容?我该怎么做?

最佳答案

可以替换

 String[] id = obj2.split("/");

 String[] id = obj2.split("/", 4);

来自javadoc :

The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

关于java - 如何在java中获取第三个斜杠之后的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37138900/

相关文章:

Java从字符串中提取多个子字符串

python - 如何操作SREC文件

c# - 如何通过索引获取字符串中的字符?

Javascript - 如何在字符串中查找子字符串并检查其前后是否有空格?

Java 第一次找到一个特定的字母

java - (n>3) 维度的 Delaunay 三角剖分

java - 如何让程序在循环内获取变量? ( java )

java - Saxon 8 - XQuery 文件 - Java 调用

java - jackson .当值从泛型类获取时出现 ClassCastException

asp.net - 子字符串的多个长度?