Split的方法很常用,除了str.split("regex"),其實還可以多傳一個參數:str.split("regex", limit)。但是要注意,JavaScript和java的split中limit參數作用是不同的。
簡單說,JavaScript中,limit是指分割后返回數組的元素個數,注意,返回值仍為數組。
Java中,limit是指regex參數用于匹配string的次數,尤其要注意,如果傳N,則將正則匹配N-1次。(不知道為什么,筆者就因為這個編碼出了bug。。)
JAVA
Splits this string around matches of the given regular expression.
This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero.
Trailing empty strings are 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" }
參數:
regex the delimiting regular expression
返回:
the array of strings computed by splitting this string around matches of the given regular expression
拋出:
PatternSyntaxException - if the regular expression's syntax is invalid
自:
1.4
另請參閱:
java.util.regex.Pattern
@spec
JSR-51
String[] java.lang.String.split(String regex, int limit)
Splits this string around matches of the given regular expression.
The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression
or is terminated by the end of the string. The substrings in the array are in the order in which they occur in this string.
If the expression does not match any part of the input then the resulting array has just one element, namely this string.
The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array.
If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n,
and the array's last entry will contain all input beyond the last matched delimiter. If n is non-positive then the pattern will be applied
as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible,
the array can have any length, and trailing empty strings will be discarded.
The string "boo:and:foo", for example, yields the following results with these parameters:
Regex Limit Result
: 2 { "boo", "and:foo" }
: 5 { "boo", "and", "foo" }
: -2 { "boo", "and", "foo" }
o 5 { "b", "", ":and:f", "", "" }
o -2 { "b", "", ":and:f", "", "" }
o 0 { "b", "", ":and:f" }
An invocation of this method of the form str.split(regex, n) yields the same result as the expression
java.util.regex.Pattern.compile(regex).split(str, n)
參數:
regex the delimiting regular expression
limit the result threshold, as described above
返回:
the array of strings computed by splitting this string around matches of the given regular expression
拋出:
PatternSyntaxException - if the regular expression's syntax is invalid
自:
1.4
另請參閱:
java.util.regex.Pattern
@spec
JSR-51
JavaScript
split 方法
將一個字符串分割為子字符串,然后將結果作為字符串數組返回。
stringObj.split([separator[,?limit]])
參數
stringObj
必選項。要被分解的?String?對象或文字。該對象不會被?split?方法修改。
separator
可選項。字符串或?正則表達式?對象,它標識了分隔字符串時使用的是一個還是多個字符。如果忽略該選項,返回包含整個字符串的單一元素數組。
limit
可選項。該值用來限制返回數組中的元素個數。
說明
split?方法的結果是一個字符串數組,在?stingObj?中每個出現?separator?的位置都要進行分解。separator?不作為任何數組元素的部分返回。
示例
下面的示例演示了?split?方法的用法。
function SplitDemo(){
var s, ss;
var s = "The rain in Spain falls mainly in the plain.";???//在每個空格字符處進行分解。
ss = s.split(" ");
return(ss);
}