不确定在这种情况下字符串分割实际上是如何工作的

发布于 2025-01-06 02:53:33 字数 653 浏览 0 评论 0原文

我没有得到以下内容:

在以下 String 中:

String s = "1234;x;;y;";

如果我这样做:
String[] s2 = s.split(";");

我得到 s2.length 为 4,

s2[0] = "1234";  
s2[1] = "x";  
s2[2] = "";  
s2[3] = "y"; 

但在字符串中: String s = "1234 ;x;y;;";

我得到:

s2.length 为 3 并且

s2[0] = "1234";  
s2[1] = "x";  
s2[2] = "y"; 

?

有什么区别,在后一种情况下我也没有得到 4 吗?

更新:
使用 -1 并不是我所期望的行为。
我的意思是最后一个分号是 String 的结尾,所以在后一个示例中我也期望 4 作为数组的长度

I don't get the following:

In the following String:

String s = "1234;x;;y;";

if I do:
String[] s2 = s.split(";");

I get s2.length to be 4 and

s2[0] = "1234";  
s2[1] = "x";  
s2[2] = "";  
s2[3] = "y"; 

But in the string: String s = "1234;x;y;;";

I get:

s2.length to be 3 and

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

少跟Wǒ拽 2025-01-13 02:53:33

来自 文档,

此方法的工作方式类似于调用双参数 split 方法
给定的表达式和零限制参数。尾随空
因此,字符串不包含在结果数组中。

更新:

您有五个由分隔的子字符串;在第二种情况下,它们是1234xy。根据文档,分割操作产生的所有空子字符串(最后)都将被消除。
有关详细信息,请参阅 这里

如果 n 为零,则该模式将被应用多次
可能的话,数组可以有任意长度,并且尾随空字符串
将被丢弃。

例如,字符串 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

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.

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.

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" }   // all the empty substrings at the end were eliminated
帥小哥 2025-01-13 02:53:33

省略尾随的空字符串。然而,如果需要的话,有一些方法可以显式地包含它们。

Trailing empty strings are omitted. However, there are ways to include them explicitly, if needed.

错爱 2025-01-13 02:53:33

来自 http:// docs.oracle.com/javase/6/docs/api/java/lang/String.html#split(java.lang.String)

此方法的工作方式类似于使用给定表达式和零限制参数调用双参数 split 方法。因此,尾随空字符串不包含在结果数组中。

From http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split(java.lang.String)

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.

胡渣熟男 2025-01-13 02:53:33

好问题。如果您检查 String.split() 的 API 文档 并使用“boo:foo”检查示例,然后您可以看到尾随的空字符串被省略。

此方法的工作方式类似于使用给定表达式和零限制参数调用双参数 split 方法。因此,尾随空字符串不包含在结果数组中。

例如,字符串“boo:and:foo”使用这些表达式会产生以下结果:

正则表达式结果

:{“嘘”,“和”,“富”}

o { "b", "", ":and:f" }

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.

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" }

甜点 2025-01-13 02:53:33

这是 java 中 split 方法的默认行为,不返回空标记。 ]

s.split("\;", -1);应该返回空令牌

Thats default behavior of split method in java to not return empty tokens . ]

s.split("\;", -1); should return empty token

悲欢浪云 2025-01-13 02:53:33

为什么不先检查文档说了什么。这是链接:

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String%29

这是您的答案:

因此,尾随空字符串不会包含在结果中
数组。

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

And here is your answer:

Trailing empty strings are therefore not included in the resulting
array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文