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);
}