How to fetch string after the third slash in java
Asked Answered
G

1

2

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?

Guinevere answered 10/5, 2016 at 12:42 Comment(1)
Can you show your input, the value of obj2 before the split?Helms
V
9

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.

Volvulus answered 10/5, 2016 at 12:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.