String[in Java Doc] Java.lang.String.split (String regex)
Splits this string around matches of the given regular expression.
This method works as if is invoking the Two-argument split method with the given expression and a limit argument of zero. Trailing empty strings is therefore not included in the resulting array.
The string "Boo:and:foo", for example, yields the following results with these expressions:
| Regex |
Result |
| : |
{"Boo", "and", "foo"} |
| O |
{"B", "", ": And:f"} |
-
-
Parameters:
-
-
regex The delimiting regular expression
-
-
Returns:
-
The
-
array of strings computed by splitting this string around matches of the given regular expression
-
-
Throws:
-
-
patternsyntaxexception-if The regular expression ' s syntax is invalid
-
-
Since:
-
-
1.4
-
-
See Also:
-
-
Java.util.regex.Pattern
-
-
@spec
-
-
JSR-51
-
-
-
but pay attention to the following points:
-
-
(1) If you use "." As a separate word must be the following string.split ("\ \"), so as to
The right separation can not be used String.Split (".");
For example, this code:
String str= "123.456"; String []arrstr=str.split ("."); System.out.println (arrstr.length);
The result is:
and
-
String str= "123.456"; String []arrstr=str.split ("\ \"); System.out.println (arrstr.length);
The result is:
Reason is
Splits this string around matches of the given regularexpression. A regular expression is required so the form of the regular expression must be satisfied,
Therefore, escaping is required.
The source code for the split () function is given below for reference:
PublicString[] Split (String regex,intlimit) { /*FastPath If the Regex is a (1) One-char String and this character are not one of the regex ' s Meta CH Aracters ". $| () [{^?*+\\], or (2) Two-char String and the first char is the backslash and the second are not the ASCII Digit or ASCII letter. */ CharCH = 0; if((Regex.value.length = = 1 && ". $| () [{^?*+\\]. indexOf (ch = regex.charat (0)) = = =-1) | |(Regex.length ()= = 2 &&Regex.charat (0) = = ' \ \ ' &&((Ch= Regex.charat (1))-' 0 ') | (' 9 '-ch)) < 0 &&(Ch-' A ') | (' Z '-ch)) < 0 &&(Ch-' A ') | (' Z '-ch)) < 0)) &&(Ch< Character.min_high_surrogate | |CH>character.max_low_surrogate)) { intOff = 0; intNext = 0; BooleanLimited = limit > 0; ArrayList<String> list =NewArraylist<>(); while(Next = indexOf (ch, off))! =-1) { if(!limited | | list.size () < limit-1) {list.add (substring (off, next)); Off= next + 1; } Else{// last one//assert (list.size () = = limit-1);list.add (substring (off, value.length)); Off=value.length; Break; } } //If no match is found, return this if(Off = = 0) return Newstring[]{ This}; //ADD remaining segment if(!limited | | list.size () <limit) list.add (substring (off, value.length)); //Construct Result intResultSize =list.size (); if(Limit = = 0) while(resultsize > 0 && list.get (resultSize-1). Length () = = 0) resultsize--; String[] Result=NewString[resultsize]; returnList.sublist (0, resultsize). ToArray (result); } returnPattern.compile (Regex). Split ( This, limit); }
Public string[] Split (String regex) {
Return split (regex, 0);
}
Java--string.split () function