简体   繁体   English

如何在 java 字符串中使用 split 方法分隔值,想要对给定的三个字符串使用相同的代码

[英]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.I - I have one query. There are three strings.I your text` want to separate locator and value. - I have one query. There are three strings.I我的文本想要分隔定位符和值。

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); } 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.但是对于字符串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); }上面的代码不适用。 i want to write common code for all three strings to separate locators and value`我想为所有三个字符串编写通用代码来分隔定位器和值`

Use indexOf("=") to find the position of the first occurrence of "=".使用indexOf("=")找到第一次出现 "=" 的 position。 Then you can simply use substring() with the corresponding indices to get locator and value.然后你可以简单地使用substring()和相应的索引来获取定位器和值。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM