Special cases have * ^: |. \
One, single symbol as delimiter
- String address= "shanghai \ Shanghai City | Minhang \ Wu Zhong Lu";
String[] Splitaddress=address.split ("\ \"); System. out. println (splitaddress[0]+splitaddress[1]+splitaddress[2]+ splitaddress[3]);
- String address= "Shanghai | shanghai | Minhang | Wu zhong Lu";
String[] Splitaddress=address.split ("\\| " // If you use a vertical bar as a delimiter, you need to add two slashes "\ \" to escape when split System. out. println (splitaddress[0]+splitaddress[1]+splitaddress[2]+ splitaddress[3]);
- String address= "Shanghai * Shanghai * Minhang * Wu Zhong Lu";
String[] Splitaddress=address.split ("\\*"); System. out. println (splitaddress[0]+splitaddress[1]+splitaddress[2]+ splitaddress[3]);
- String address= "Shanghai: Shanghai: Minhang: Wu Zhong Lu";
String[] Splitaddress=address.split ("\ \:"); System. out. println (splitaddress[0]+splitaddress[1]+splitaddress[2]+splitaddress[ 3]);
- String address= "Shanghai. Minhang. Wu Zhong Lu, Shanghai, China";
String[] Splitaddress=address.split ("\ \. " ); System. out. println (splitaddress[0]+splitaddress[1]+splitaddress[2]+splitaddress[ 3]);
- String address= "Shanghai ^ Shanghai ^ Minhang ^ Wu Zhong Lu";
String[] Splitaddress=address.split ("\\^"); System. out. println (splitaddress[0]+splitaddress[1]+splitaddress[2]+splitaddress[ 3]);
- String address= "Shanghai @ Shanghai @ Minhang @ Wu Zhong Lu";
String[] Splitaddress=address.split ("@"); System. out. println (splitaddress[0]+splitaddress[1]+splitaddress[2]+splitaddress[ 3]);
- String address= "Shanghai, Shanghai, Minhang, Wu Zhong Lu";
String[] Splitaddress=address.split (","); System. out. println (splitaddress[0]+splitaddress[1]+splitaddress[2]+splitaddress[ 3]);
Two or more symbols as separators
String address= "Shanghai ^ Shanghai @ Minhang # Wu Zhong Lu";
String[] Splitaddress=address.split ("\\^|@|#"); System. out. println (splitaddress[0]+splitaddress[1]+splitaddress[2]+splitaddress[ 3 ]);
Example:
New String ("192.168.13.240"); = Address.split ("\ \. " ); for (String s:str) { System. out . println (s); }
output Format:192168 - -
Summary: (1) The split expression is actually a regular expression. * ^ | A symbol is a character in a regular expression that has a special meaning, and if used as a delimiter, it must be escaped with the escape character, \ \. (2) If you use more than one delimiter, you need the help of the | symbol, as shown in two, but the escape character needs to be added to the delimiter to handle
Java Split () usage