英文:
How could the split() for Strings in Java be explained when two equal characters reside adjacently
问题
以下是要翻译的内容:
在 String.split
的文档中,
以下是示例。最后一个示例如何解释?
例如,字符串 "boo:and:foo" 在这些参数下产生以下结果:
正则表达式 限制 结果
: 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" }
英文:
In the docs for String.split
,
The folowing are the examples. How can the last example be explained?
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" }
答案1
得分: 1
仔细阅读文档:
该方法返回的数组包含了此字符串的每个子字符串,这些子字符串以与给定表达式匹配的另一个子字符串终止,或以字符串的结尾终止。
请注意,空字符串是任何字符串的子字符串,包括 boo:and:foo
。如果你执行 "boo:and:foo".substring(2, 2)
,会得到空字符串。在第前两个 o 之间的空字符串后面紧跟着(即“被终止于”)子字符串 “o”(第二个 o)。子字符串 “o” 与正则表达式 “o” 匹配,因此空字符串满足要求:
被与给定表达式匹配的另一个子字符串终止,或被字符串结尾终止
因此它被放入结果数组中。
倒数第二个 o 之后的空字符串也满足这一条件,而最后一个 o 之后的空字符串“被字符串结尾终止”。它们本应被添加到数组中,数组会变成:
{ "b", "", ":and:f", "", "" }
然而,它们从数组中被丢弃了,因为:
如果 [limit] 为零,则模式将尽可能多地应用,数组的长度可以是任意的,尾部的空字符串将被丢弃。
“尾部的空字符串” 指的是数组中的最后两个 ""
元素,它们被丢弃了。
英文:
Read the docs carefully:
> 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.
Note that the empty string is a substring of any string, including boo:and:foo
. If you do "boo:and:foo".substring(2, 2)
, you will get the empty string. The empty string between the first two o's is followed by (i.e. "is terminated by") the substring "o" (the second o). The substring "o" matches the regex "o", so the empty string fulfils the requiremen:
> is terminated by another substring that matches the given expression or is terminated by the end of the string
So it gets put into the resulting array.
The empty string after the second to last o also fulfils this criteria, and the empty string after the last o "is terminated by the end of the string". They should have been added to the array, and the array would have looked like:
{ "b", "", ":and:f", "", "" }
However, they are discarded from the array, because,
> If [limit] 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.
"trailing empty strings" refers to the last two ""
elements in the array, which gets discarded.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论