gpt4 book ai didi

java - 根据android中的一些特殊字符将一个字符串一分为二

转载 作者:行者123 更新时间:2023-11-29 20:38:51 26 4
gpt4 key购买 nike

我有这样一个字符串:

[{\"ID\":2,\"Text\":\"Capital Good\"},{\"ID\":3,\"Text\":\"General Office Items\"},{\"ID\":1,\"Text\":\"Raw Material Purchase\"}]&@[{\"ID\":2,\"Text\":\"Capital Good\"},{\"ID\":3,\"Text\":\"General Office Items\"},{\"ID\":1,\"Text\":\"Raw Material Purchase\"},{\"ID\":0,\"Text\":\"Approved\"},{\"ID\":1,\"Text\":\"Freezed\"},{\"ID\":2,\"Text\":\"Cancelled\"},{\"ID\":3,\"Text\":\"Completed\"},{\"ID\":4,\"Text\":\"Foreclosed\"},{\"ID\":5,\"Text\":\"UnApproved\"}]

我想根据 &@ 字符组合将我的字符串分成两部分,如下所示:

 String [] separated=line.split("&@");

但是当我检查值时,separated[0] 包含完美的字符串的前半部分,但是当我检查 separated[1] 时,它包含没有“&@”字符的完整字符串。我想分隔 [1] 在“&@”之后包含字符。任何帮助将不胜感激。

最佳答案

了解split函数,它将输入定界并拆分字符串。

字符串[] java.lang.String.split(字符串正则表达式)

围绕给定正则表达式的匹配拆分此字符串。

此方法的工作方式就好像通过使用给定表达式和零限制参数调用双参数拆分方法一样。因此,结果数组中不包含尾随的空字符串。

例如,字符串“boo:and:foo”使用这些表达式会产生以下结果:

正则表达式结果:{“嘘”,“和”,“富”}o { "b", "", ":and:f"}

如果你想要分隔符(“&@”)出现。试试这个

public static void main(String[] args) {
String input="[{\"ID\":2,\"Text\":\"Capital Good\"},{\"ID\":3,\"Text\":\"General Office Items\"},{\"ID\":1,\"Text\":\"Raw Material Purchase\"}]&@[{\"ID\":2,\"Text\":\"Capital Good\"},{\"ID\":3,\"Text\":\"General Office Items\"},{\"ID\":1,\"Text\":\"Raw Material Purchase\"},{\"ID\":0,\"Text\":\"Approved\"},{\"ID\":1,\"Text\":\"Freezed\"},{\"ID\":2,\"Text\":\"Cancelled\"},{\"ID\":3,\"Text\":\"Completed\"},{\"ID\":4,\"Text\":\"Foreclosed\"},{\"ID\":5,\"Text\":\"UnApproved\"}]";
String[] resultArray = input.split("&@");
for(int i=0;i<resultArray.length;i++){
if(i!=0)
resultArray[i]="&@"+resultArray[i];
}

for(String str:resultArray)
System.out.println(str);
}

关于java - 根据android中的一些特殊字符将一个字符串一分为二,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31176675/

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