How to fetch string after the third slash in java

二次信任 提交于 2019-12-12 03:03:39

问题


I am trying to fetch the string after the third slash. But i don' know how to do it. I have used split but that's not what I want.

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] gives me only id[3] but i want everything after the third slash? how can i do that?


回答1:


You can replace

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

by

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

From the 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.



来源:https://stackoverflow.com/questions/37138900/how-to-fetch-string-after-the-third-slash-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!