Java "split(regEx)" 和 "split(regEx, 0)" 的区别?
Java difference between "split(regEx)" and "split(regEx, 0)"?
使用split(regEx)
和split(regEx, 0)
有什么区别吗?
因为输出是针对我测试过的情况。例如:
String myS = this is stack overflow;
String[] mySA = myS.split(' ');
结果 mySA === {'this','is','stack,'overflow'}
和
String myS = this is stack overflow;
String[] mySA = myS.split(' ', 0);
也会导致 mySA === {'this','is','stack,'overflow'}
"hidden"这里有什么事吗?或者关于 .split(regEx, 0)
?
需要了解的其他信息
它们本质上是一样的。
引自 String.split(String regex)
文档:
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.
回答问题。是的,它们是一样的。
请找到实习生调用split(regex,0)
方法的Stringclass的split方法。
public String[] split(String regex) {
return split(regex, 0);
}
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
例如下面的代码可以给你一些启发。
String myS = "this is stack overflow";
String[] mySA = myS.split(" ", 2);
String[] withOutLimit = myS.split(" ");
System.out.println(mySA.length); // prints 2
System.out.println(withOutLimit.length); // prints 4
使用split(regEx)
和split(regEx, 0)
有什么区别吗?
因为输出是针对我测试过的情况。例如:
String myS = this is stack overflow;
String[] mySA = myS.split(' ');
结果 mySA === {'this','is','stack,'overflow'}
和
String myS = this is stack overflow;
String[] mySA = myS.split(' ', 0);
也会导致 mySA === {'this','is','stack,'overflow'}
"hidden"这里有什么事吗?或者关于 .split(regEx, 0)
?
它们本质上是一样的。
引自 String.split(String regex)
文档:
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.
回答问题。是的,它们是一样的。
请找到实习生调用split(regex,0)
方法的Stringclass的split方法。
public String[] split(String regex) {
return split(regex, 0);
}
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
例如下面的代码可以给你一些启发。
String myS = "this is stack overflow";
String[] mySA = myS.split(" ", 2);
String[] withOutLimit = myS.split(" ");
System.out.println(mySA.length); // prints 2
System.out.println(withOutLimit.length); // prints 4