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:
Split method
Splits a string into substrings and returns the result as an array of strings.
Stringobj.split ([Separator,[limit]])
Required option. The String object or text to be decomposed, and the object is not modified by the split method.
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.
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:
1 String str= "Java string split test"2 string[] Strarray=str.split (""3 for (int i = 0; i < strarray.length; i++4
Will output:
Java
String
Split
Test
Example 2:
1 String str= "Java string split test"2 string[] Strarray=str.split ("", 2); 3 for (int i = 0; i < strarray.length; i++ 4
Will output:
Java
String Split test
Example 3:
1 String str= "192.168.0.1"2 string[] Strarray=str.split ("." 3 for (int i = 0; i < strarray.length; i++4
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;
String splitting functions in Java