Use split and split

Source: Internet
Author: User

Use split and split

Java. lang. string. split
Split Method
Splits a string into substrings and returns the result as a string array.
StringObj. split ([separator, [limit])
StringObj
Required. The String object or text to be decomposed. The object is not 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 (that is, it can be divided into several array elements at most, and only has an impact on the positive number)
The result of the split method is a string array. In stingObj, the position where separator appears must be decomposed. The separator is not returned as part of any array element.
Example 1:
String str = "Java string split test ";
String [] strarray = str. split ("");
For (int I = 0; I <strarray. length; I ++)
System. out. println (strarray [I]);
Output:
Java
String
Split
Test

Example 2:
String str = "Java string split test ";
String [] strarray = str. split ("", 2); // use limit to split up to two strings
For (int I = 0; I <strarray. length; I ++)
System. out. println (strarray [I]);
Output:
Java
String split test

Example 3:
String str = "192.168.0.1 ";
String [] strarray = str. split (".");
For (int I = 0; I <strarray. length; I ++)
System. out. println (strarray [I]);
No output is returned. Change split (".") to split ("\.") and the correct result is output:
192
168
0
1

Multiple symbols are used as the separator String address = "Shanghai ^ Shanghai @ Minhang District # wuzhong Road"; String [] splitAddress = address. split ("\ ^ | @ | #"); System. out. println (splitAddress [0] + splitAddress [1] + splitAddress [2] + splitAddress [3]);
/* The article to be split */String str = "first sentence. Second sentence! Third sentence: fourth sentence; fifth sentence. ";/* Regular Expression: Sentence Terminator */String regEx =": |. |! |; "; Pattern p = Pattern. compile (regEx); Matcher m = p. matcher (str );

Experience Sharing:
1. The separator is ". "(no output)," | "(cannot get the correct result) When escape characters," * "," + "error throws an exception, "\" must be added in front of all, such as split (\ | );
2. If "\" is used as the separator, it must be written as follows: String. split ("\\\\"), because in Java, "\" is used to represent "\", the string must be written as follows: string Str = "a \ B \ c ";
? Escape characters, which must contain "\\";
3. If a String contains multiple separators, you can use "|" as a hyphen, for example, string str = "Java String-split # test". You can use Str. split ("|-| #") separates each string;

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.