Usage analysis of Java String.Split () function

Source: Internet
Author: User

The prototype of the String.Split () method in the Java.lang package is:
Public string[] Split (String regex, int limit)
The Split function is used to separate strings into an array of strings using a specific cut character (regex), and the function return is an array. The location in which each regex occurs is decomposed.
It is important to note that there are the following points:
(1) Regex is optional. A string or regular expression object that identifies whether one or more characters are used when separating the string. If this option is omitted, a single element array containing the entire string is returned.
(2) Limit is also an option. This value is used to limit the number of elements in the returned array.
(3) To note the escape character: "." and "|" is an escape character and must be added "\ \". Similarly: * and + are also true.
If you use "." As a separate word, it must be written as follows:
String.Split ("\ \"), in order to properly separate, can not be used String.Split (".");
If you use the ' | ' As a separate word, it must be written as follows:
String.Split ("\\|"), so as to properly separate, can not use String.Split ("|");
(4) If you have more than one delimiter in a string, you can use the ' | ' As a hyphen, for example: "Acountid=?" and act_id =? Or extra=? ", separating the three, can be used
String.Split ("And|or");
(5) The result of the split function is closely related to the regex, and several common cases are as follows:

1234567891011121314151617181920212223242526272829303132333435 public class splittest { public static void main(String[] args) {String str1 = "A-B"; String str2 = "a-b-"; String str3 = "-a-b"; String str4 = "-a-b-"; String str5 = "a"; String str6 = "-"; String str7 = "--"; String str8 = ""; split(str1); split(str2); split(str3); split(STR4); split(STR5); split(STR6); split(str7); split(str8); }public static void split(String demo){ String[] array = demo. Split("-"); int len = arraylength system. Out. Print ( "\" " + demo + "\" After the split length is: " + len) if(len >= 0) {System. Out. Print(", the result of the split is:"); for(int i=0; I<len; I+ +) {System. Out. Print("\"+array[i]+"\" "); }}System. Out. println(); }}

The result of the operation is:
The length of the "A-B" split is: 2, the result of the split is: "A" "B."
"a-b-" After the split length is: 2, after the result is: "A" "B"
"-a-b" After the split length is: 3, after the result is: "" "A" "B"
"-a-b-" After the split length is: 3, after the result is: "" "A" "B"
"A" after the split length is: 1, after the result is: "A"
"-" After the split length is: 0, after the result is:
"--" after the split length is: 0, the result of the split is:
"" After the split length is: 1, the result of the split is: ""
This can be derived from:
When the string contains only delimiters, the returned array has no elements;
When a string does not contain a delimiter, the returned array contains only one element (the string itself);
The delimiter at the end of the string can be seen as nonexistent and does not affect the separation of strings;
The delimiter that appears at the front of the string separates an empty string from the normal separation of the remainder;

Usage analysis of Java String.Split () function

Usage analysis of the Java String.Split () function

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.