Tips for using the Split function in Java

Source: Internet
Author: User

The String. split () method is also available in the java. lang package. Similar to. net, the return is a struct-type array, but there are some tips during use. I will introduce you as follows. For more information, see

For example, Run "2 | 33 | 4". split ("| ")
The result is:
""
2

3
3

4
It's strange, but check the API description to find out why.

Java. lang. string. split
Split Method
Splits a string into substrings and returns the result as a string array.
StringObj. split ([separator, [limit])
Parameters
StringObj
Required. String object or text to be decomposed. This object will not be modified by the split method.
Separator
Optional. A string or regular expression object that identifies whether a string is separated by one or more characters. If this option is ignored, a single array of elements containing the entire string is returned.
Limit
Optional. This value is used to limit the number of elements in the returned array.
Description
The result of the split method is a string array. In stingObj, the position where separator appears must be decomposed.

Therefore, the normal syntax is as follows:
1. If ". ", must be written as follows: String. split (". "), in this way, the correct separation is not allowed using String. split (". ");
2. If "|" is used as the separator, it must be written as follows: String. split ("|"), so that the correct separation is not allowed, String. split ("| ");
"." And "|" are escape characters and must be added "";
3. If a string contains multiple separators, you can use "|" as a hyphen, for example, "a = 1 and B = 2 or c = 3 ", you can use String to separate all three. split ("and | or ");

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.