In Java, we can use the Split method (Java.lang.string.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.
Split method
Splits a string into substrings and returns the result as an array of strings.
Stringobj.split ([Separator,[limit]])
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.
Note: Separator is not returned as part of any array element.
Example 1 Basic usage:
1 String str = "Nice to meet You"; 2 string[] Strarray = Str.split (""); 3 4 // Note the use of the Foreach Loop 5 for (String string:strarray) {6 System.out.println (string); 7 }
Operation Result:
Instance 2 to add a string number:
1 String str = "Nice to meet You"; 2 string[] Strarray = Str.split ("", 2); // 34 for (int i = 0; i < strarray.length; i++) {5 System.out.println (Strarray[i]); 6 }
Operation Result:
Instance 3 Special symbol delimiter _1:
1 String str = "192.168.0.0"; 2 string[] Strarray = str.split ("\ \.") ); 3 4 for (int i = 0; i < strarray.length; i++) {5 System.out.println (Strarray[i]); /c12>6 }
Operation Result:
Instance 4 special symbol delimiter _2:
1 String str = "Nice\\to\\meet\\you"; 2 string[] Strarray = Str.split ("\\\\"); 3 4 for (String string:strarray) {5 System.out.println (string); 6 }
Operation Result:
Instance 5 hyphen:
1 String str = "Nice&to%meet#you"; 2 string[] Strarray = Str.split ("&|#|%"); // Note that there is no back and forth order 3 4 for (String string:strarray) {5 System.out.println (string); 6 }
Operation Result:
Precautions:
1. The delimiter is ".": No output
"|": Cannot get the correct result) when escaping characters
"*", "+" when an error throws an exception, must be preceded by adding "\ \", such as split (\\|) (see Example 3).
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 "\ \"; (see Example 4)
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; (see example 5)
Main finishing from: http://blog.csdn.net/huangxy10/article/details/8186574
Java String Separation Split