Search code examples
javasubstringlastindexof

java String query


i have a requirement i needed to be figure out .

consider a string for ex "/XXXXXXXXX/XXXXXXXXX/XXXXXXXXX/XXXXXXX ......n times "

I needed to get the second set of string from this string,last string set,with out last string set rest of the String. xx mentioned in example can be a-z,A-z,0-9,and spaces also. i am able to get last string set,with out last string set rest of the String. please let me know how to get 2nd string set.

              String a ="/WorkGroups/Testing Test WG 1/R14A";
              int position = a.lastIndexOf('/');
              System.out.println(position);//Print 28 
              System.out.println(a.substring(0,position));//print /WorkGroups/EriDoc Test WG 1
              System.out.println(a.substring(position+1));//print R14A

how to get only Testing Test WG 1 ?


Solution

  • Here is the code -

        String a ="/WorkGroups/Testing Test WG 1/R14A";
        System.out.println(a.split("/")[2]);
    

    Output -

     Testing Test WG 1