gpt4 book ai didi

java - Split 方法忽略末尾的逗号

转载 作者:行者123 更新时间:2023-12-01 23:25:30 25 4
gpt4 key购买 nike

当我用多个逗号分割一个字符串时,它们在数组中被忽略。这是对的吗 ?

    String a = "a,b,c,,f,,";
String[] sdf = a.split(",");
for (int i = 0; i < sdf.length; i++) {
System.out.println(sdf[i]);
}

或者我错过了什么。

我得到的输出是

[a,b,c, ,f]   

我想考虑这些空白值,期待这样的事情

[a,b,c, ,f, ,] 

最佳答案

您需要使用:

public String[] split(String regex,
int limit)

并说:

String[] sdf = a.split(",", -1);

对于您的输入,它会产生:

[a, b, c, , f, , ]

引自documentation :

If n is non-positive then the pattern will be applied as many times as possible and the array can have any length.

关于java - Split 方法忽略末尾的逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20068416/

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