gpt4 book ai didi

java - String的split方法可能出错

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:50:07 24 4
gpt4 key购买 nike

我有下一个代码:

String test = "A,B,,,,,";

String[] result = test.split(",");

for(String s : result){
System.out.println("$"+s+"$");
}

输出是:

$A$
$B$

我期望:

$A$
$B$
$$
$$
$$
$$
$$

但是,我修改了如下代码:

String test = "A,B,,,,,C";

String[] result = test.split(",");

for(String s : result){
System.out.println("$"+s+"$");
}

结果是:

$A$
$B$
$$
$$
$$
$$
$C$

其他变化:

String test = "A,B,,,C,,";

String[] result = test.split(",");

for(String s : result){
System.out.println("$"+s+"$");
}

结果:

$A$
$B$
$$
$$
$C$

有什么想法吗?

我需要这个来将 csv 文件转换为 java 对象,但是当他们没有向我发送最后一列时,代码无法正常工作

最佳答案

来自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.

关于java - String的split方法可能出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20269024/

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