I don't get the following:
我不明白以下几点:
In the following String
:
在以下字符串:
String s = "1234;x;;y;";
字符串s =“1234;x、y”;
if I do:String[] s2 = s.split(";");
如果是:String[] s2 = s.s split(";");
I get s2.length
to be 4 and
我得到s2。长度为4。
s2[0] = "1234";
s2[1] = "x";
s2[2] = "";
s2[3] = "y";
But in the string: String s = "1234;x;y;;";
但在字符串中:string s = "1234;x;y; ";
I get:
我得到:
s2.length
to be 3 and
s2。长度为3。
s2[0] = "1234";
s2[1] = "x";
s2[2] = "y";
?
吗?
What is the difference and I don't get 4 in the latter case as well?
后一种情况下,我没有得到4,有什么区别?
UPDATE:
Using -1
is not was I was expecting as behavior.
I mean the last semicolon is the end of the String
so in the latter example I was also expecting 4
as length of the array
更新:使用-1不是我期望的行为。我的意思是最后一个分号是字符串的末尾,所以在后面的例子中,我也期望数组的长度是4
6 个解决方案
#1
10
From the docs,
的文档,
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.
该方法通过调用具有给定表达式和限制参数为0的双参数分割方法来工作。因此,结果数组中不包含尾空字符串。
UPDATE:
更新:
You have five substrings separated by ;
In the second case, these are 1234
, x
, y
, and
. As per the docs, all empty substrings (at the end) which result from the split operation would be eliminated. For details, look here.
有5个子字符串;在第二种情况下,这些是1234 x y和。根据文档,所有由分割操作产生的空子字符串(最后)都将被删除。详情,请看这里。
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.
如果n为0,那么模式将被尽可能多地应用,数组可以有任意长度,拖尾空字符串将被丢弃。
The string boo:and:foo
, for example, yields the following results with these parameters:
例如,字符串boo:and:foo通过这些参数产生如下结果:
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" } // all the empty substrings at the end were eliminated
#2
4
Trailing empty strings are omitted. If you need them use the approach described in http://www.xinotes.org/notes/note/226/
尾空字符串被省略。如果需要,请使用http://www.xinotes.org/notes/note/226/中描述的方法
#3
3
From http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split(java.lang.String)
从http://docs.oracle.com/javase/6/docs/api/java/lang/String.html分裂(以)
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.
该方法通过调用具有给定表达式和限制参数为0的双参数分割方法来工作。因此,结果数组中不包含尾空字符串。
#4
2
Good question. If you check the API documentation for String.split()
and check the example with "boo:foo" then you can see that the trailing empty strings are omitted.
好问题。如果您检查String.split()的API文档并使用“boo:foo”检查示例,那么您可以看到末尾的空字符串被省略了。
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.
该方法通过调用具有给定表达式和限制参数为0的双参数分割方法来工作。因此,结果数组中不包含尾空字符串。
The string "boo:and:foo", for example, yields the following results with these expressions:
例如,字符串“boo:and:foo”将使用以下表达式生成以下结果:
Regex Result
正则表达式的结果
: { "boo", "and", "foo" }
: {"boo"和"foo"}
o { "b", "", ":and:f" }
o {"b", "", ":和:f"}
#5
1
Thats default behavior of split method in java to not return empty tokens . ]
这是java中split方法不返回空令牌的默认行为。]
s.split("\;", -1); should return empty token
s.split(“\;",1);应该返回空令牌
#6
0
Why not check what does the documention says first. Here is the link:
为什么不先检查一下文件上写的是什么?这是链接:
http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String%29
http://docs.oracle.com/javase/6/docs/api/java/lang/String.html分裂% 28以% 29
And here is your answer:
这是你的答案:
Trailing empty strings are therefore not included in the resulting array.
因此,结果数组中不包含尾空字符串。
#1
10
From the docs,
的文档,
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.
该方法通过调用具有给定表达式和限制参数为0的双参数分割方法来工作。因此,结果数组中不包含尾空字符串。
UPDATE:
更新:
You have five substrings separated by ;
In the second case, these are 1234
, x
, y
, and
. As per the docs, all empty substrings (at the end) which result from the split operation would be eliminated. For details, look here.
有5个子字符串;在第二种情况下,这些是1234 x y和。根据文档,所有由分割操作产生的空子字符串(最后)都将被删除。详情,请看这里。
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.
如果n为0,那么模式将被尽可能多地应用,数组可以有任意长度,拖尾空字符串将被丢弃。
The string boo:and:foo
, for example, yields the following results with these parameters:
例如,字符串boo:and:foo通过这些参数产生如下结果:
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" } // all the empty substrings at the end were eliminated
#2
4
Trailing empty strings are omitted. If you need them use the approach described in http://www.xinotes.org/notes/note/226/
尾空字符串被省略。如果需要,请使用http://www.xinotes.org/notes/note/226/中描述的方法
#3
3
From http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split(java.lang.String)
从http://docs.oracle.com/javase/6/docs/api/java/lang/String.html分裂(以)
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.
该方法通过调用具有给定表达式和限制参数为0的双参数分割方法来工作。因此,结果数组中不包含尾空字符串。
#4
2
Good question. If you check the API documentation for String.split()
and check the example with "boo:foo" then you can see that the trailing empty strings are omitted.
好问题。如果您检查String.split()的API文档并使用“boo:foo”检查示例,那么您可以看到末尾的空字符串被省略了。
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.
该方法通过调用具有给定表达式和限制参数为0的双参数分割方法来工作。因此,结果数组中不包含尾空字符串。
The string "boo:and:foo", for example, yields the following results with these expressions:
例如,字符串“boo:and:foo”将使用以下表达式生成以下结果:
Regex Result
正则表达式的结果
: { "boo", "and", "foo" }
: {"boo"和"foo"}
o { "b", "", ":and:f" }
o {"b", "", ":和:f"}
#5
1
Thats default behavior of split method in java to not return empty tokens . ]
这是java中split方法不返回空令牌的默认行为。]
s.split("\;", -1); should return empty token
s.split(“\;",1);应该返回空令牌
#6
0
Why not check what does the documention says first. Here is the link:
为什么不先检查一下文件上写的是什么?这是链接:
http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String%29
http://docs.oracle.com/javase/6/docs/api/java/lang/String.html分裂% 28以% 29
And here is your answer:
这是你的答案:
Trailing empty strings are therefore not included in the resulting array.
因此,结果数组中不包含尾空字符串。