gpt4 book ai didi

java - split() 分隔符之间没有任何内容 = 空字符串

转载 作者:行者123 更新时间:2023-12-02 08:44:44 25 4
gpt4 key购买 nike

想象你有:

String string = "A;B;;D";
String[] split = string.split(";");

我想要的结果是:

split[0] = "A";
split[1] = "B";
split[2] = "";
split[3] = "D";

但结果是:

split[0] = "A";
split[1] = "B";
split[2] = "D";

有一个简单的正确方法吗?

最佳答案

使用重载方法split(String regex, int limit) :

String string = "A;B;;D";
String[] split = string.split(";", -1);
<小时/>

来自documentation :

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

关于java - split() 分隔符之间没有任何内容 = 空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11750202/

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