在Java中,当两个相同的字符相邻时,如何解释字符串的split()方法。

huangapple go评论84阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年9月19日 13:13:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63965454.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定