有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java split()方法混淆

我不明白为什么它是这样工作的——请参见下面的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]

我希望他们打印[一,]和[一]


共 (1) 个答案

  1. # 1 楼答案

    的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, ]