String splitting in Java.

Source: Internet
Author: User

Transfer from http://blog.csdn.net/yuwenhao0518/article/details/7161059

http://longkm.blog.163.com/blog/static/116662640200971541741/

The Split function in Java is not the same as the Split function in JS.

In Java, we can use split to split a string into a specified delimiter, and then return an array of strings, followed by an example of string.split usage and considerations:
Java.lang.string.split
Split method
Splits a string into substrings and returns the result as an array of strings.
Stringobj.split ([Separator,[limit]]) free Resource Collection network (http://www.freezq.cn)
Stringobj
Required option. The String object or text to be decomposed, and the object is not modified by the split method.
Separator
Options are available. 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.
Limit
Options are available. This value is used to limit the number of elements in the returned array (that is, most of them are split into several array elements, only if they are positive)
The result of the split method is an array of strings, which are decomposed in the stingobj where each occurrence of the separator occurs. 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]);
Will output:
Java
String
Split
Test

Example 2:
String str= "Java string split test";
String[] Strarray=str.split ("", 2);//use limit to split up to 2 strings
for (int i = 0; i < strarray.length; i++)
System.out.println (Strarray[i]);
Will 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]);
The result is nothing output, will split (".") Instead of split ("\ \"), the correct result will be output:
192
168
0
1

Experience Sharing:
1, the delimiter is "." (No output), "|" (Cannot get the correct result) when the escape character, "*", "+" when an error throws an exception, must be preceded by adding "\ \", such as split (\\|);
2, if "\" as a separate, you have to write this: String.Split ("\\\\"), because in Java is "\ \" to denote "\", the string must be written like this: string str= "a\\b\\c";
The escape character must be added "\ \";
3. If you have more than one delimiter in a string, you can use the ' | ' As a hyphen, for example: string str= "Java string-split#test", you can use Str.split ("|-|#") to separate each string;

Issues to be aware of when using the String.Split method

When you use the String.Split method to separate strings, separators may not get the results we expect if they use some special characters. We look at the JDK Doc stating public string[] Split (String regex) splits this string around matches of the given regular expression. Parameter regex is a regular-expression matching pattern instead of a simple string, and he may have unexpected results for some special characters, such as testing the following code: using a vertical bar | Separating the strings, you will not get the expected results string[] AA = "AAA|BBB|CCC". Split ("|");
string[] aa = "AAA|BBB|CCC". Split ("\\|"); In order to get the correct string result, running with a vertical * delimited string will throw a Java.util.regex.PatternSyntaxException exception, with a Plus +. string[] aa = "AAA*BBB*CCC". Split ("*");
string[] aa = "AAA|BBB|CCC". Split ("\\*"); In order to get the correct string result, it is obvious that the + * is not a valid pattern-matching rule expression, and the correct string result can be obtained after escaping with "\\*" "\\+". The "|" string is executed, but it is not intended, resulting in the segmentation of each character, not the strings, "\\|" After escaping, you get the correct string result. Also, if you want to use the "\" character in a string, you also need to escape. First to express "AAAA\BBBB" This string should be used "AAAA\\BBBB", if you want to separate it should be so to get the correct result: string[] aa = "AAA\\BBB\\BCCC". Split ("\\\\");
Note When using "." When separating, use "[.]" to separate!

String splitting in Java.

Related Article

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.