Java drip record 1
The use of split in 1.Java
In Java we can use split to split a string into a specified delimiter, and then return an array of strings, following the usage examples of string.split 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]]) stringobj required option. The String object or text to be decomposed, and the object is not modified by the split method. Separator options 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. The limit option is available. This value is used to limit the number of elements in the returned array (that is, to split up into several array elements, only for positive numbers) The result of the split method is an array of strings, which is decomposed for each occurrence of separator in stingobj. 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 2 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]); 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;
Java drip record 1