Java Split () usage

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.