srikanth reddy
srikanth reddy

Reputation: 1

How to separate values using split method in java Strings ,want use same code for three Strings given

- I have one query. There are three strings .Iyour text` want to separate locator and value .

String s1 = "id=txtussername"; String s2 = "CSSselector=#txtusername"; String s3 = "xpath=//tagnme\[@att='cytrix'\]"; String[] words = s1.split("="); for (String word : words) { System.out.println(word); } but for string s3 the above code is not applicable. i want to write common code for all three strings to separate locators and value`

Upvotes: 0

Views: 60

Answers (1)

Turamarth
Turamarth

Reputation: 2282

Use indexOf("=") to find the position of the first occurrence of "=". Then you can simply use substring() with the corresponding indices to get locator and value.

String s = "string=multiple=equals";
int sep_Pos = s.indexOf("=");
String locator = s.substring(0, sep_Pos);
String value = s.substring(sep_Pos+1); 

Upvotes: 1

Related Questions