我正在尝试获取第三个斜杠之后的字符串。但我不知道该怎么做。我使用过 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/