Rakesh Choudhury
Rakesh Choudhury

Reputation: 3

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 ?

Upvotes: 0

Views: 93

Answers (3)

Spartan01
Spartan01

Reputation: 66

This is an option :

System.out.println(a.substring(12, 28)

First parameter is an index of a start element, second parameter is an index of last element.

Upvotes: 0

Ninad Pingale
Ninad Pingale

Reputation: 7069

Here is the code -

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

Output -

 Testing Test WG 1

Upvotes: 0

TheLostMind
TheLostMind

Reputation: 36304

String[] arr = yourString.split("/");
arr[whateverIndexYouWant]

Upvotes: 1

Related Questions