简体   繁体   中英

How to split to string Java

I have problem with split String Java this my string

 String valume="<z1> 0176543210010005160D2001000</z1 <z2>S4P6W7M522SC3OXX55K3NN77666N34M2</z2><z3>Moja Karta</z3><z4>90</z4>";

How to split this string have have two strings?

string1 = 0176543210010005160D2001000

string2 = S4P6W7M522SC3OXX55K3NN77666N34M2
string3=Moja Karta
string4=90

I guess something like this should work. But i don't really understood why you would do something like that

String wait =("<z1>0176543210010005160D2001000</z1>
               <z2>S4P6W7M522SC3OXX55K3NN77666N34M2</z2>");

String string1 = wait.split("</z1><z2>")[0].replace("<z1>","");
String string2 = wait.split("</z1><z2>")[1].replace("</z2>","");
System.out.println(string1);
System.out.println(string2);

You can use regex to split your string if it follows a pattern as you mentioned in your example.

 String value="<z1>0176543210010005160D2001000</z1>
               <z2>S4P6W7M522SC3OXX55K3NN77666N34M2</z2>";
 Pattern p = Pattern.compile("<z[0-9]>(.+?)</z[0-9]>");
 Matcher m = p.matcher(value);
 while(m.find())
 {
      String result = m.group(1);
      System.out.println(result);
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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