Split method for string objects in Java

Source: Internet
Author: User

  

There is a String.Split () method in the Java.lang package, which returns a string[] array, and today encountered a problem that he did not notice:

1. Special separators

String str1 = "123|456|789"; System.out.println (Str1.split ("|") [0]);

The result is 1.

The note here is "|" As a delimiter to write like this

System.out.println (Str1.split ("\\|") [0]);

Similarly, if you use "." The delimiter is also called plus 2 backslashes,

2. Multiple separators

String str1 = "123 aa 456 bb 789"; System.out.println (Str1.split ("AA|BB") [0]); System.out.println (Str1.split ("AA|BB") [1]); System.out.println (Str1.split ("AA|BB") [2]);

The result is 123.

456

789

Multiple separators can be used with the ' | ' Link up

Reference blog:

Http://www.cnblogs.com/mingforyou/archive/2013/09/03/3299569.html

Split method for string objects in Java

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.