gpt4 book ai didi

Java split() 方法混淆

转载 作者:行者123 更新时间:2023-11-30 06:50:42 25 4
gpt4 key购买 nike

我不明白为什么它会这样工作 - 请参阅下面的 split() 方法的用例。

    String x = "one,";
String y = ",one";
System.out.println(Arrays.toString(x.split(","))); //prints [one]
System.out.println(Arrays.toString(y.split(","))); //prints [, one]

我希望他们打印 [one, ] 和 [, one]。

最佳答案

split 的 Javadoc 回答说:

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.

x.split(",",-1) 将产生 [one, ]

关于Java split() 方法混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41136104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com