Usage analysis of Java String.Split () function __java

Source: Internet
Author: User
usage points for Homjava string.split () function

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 a string into an array of strings using a specific cutter character (regex), and the function returns an array. In each position where the regex appears, it is decomposed.
Note that there are several points to follow:
(1) Regex is optional. A string or regular expression object that identifies whether one or more characters are used to delimit a string. If this option is omitted, an array of single elements containing the entire string is returned.
(2) limit is also optional. This value is used to limit the number of elements returned in an array.
(3) Note the escape character: "." and "|" are escape characters and must be added "\". Similarly: * and + are also so.
If you use the "." As a separate word, it must be worded as follows:
String.Split ("\"), so that the correct separation, can not use String.Split (".");
If you use the "|" As a separate word, it must be worded as follows:
String.Split ("\\|"), so as to correctly separate, can not use String.Split ("|");
(4) If you have more than one separator in a string, you can use the "|" As a hyphen, such as: "Acountid=?" and act_id =? Or extra=? ", separate the three, you can use
String.Split ("And|or");
(5) The result of the split function is closely related to the regex, and several common situations are as follows:

The
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3 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 = "--";

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.