gpt4 book ai didi

java - 在java中提取大括号之间的字符串

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

我有字符串{a,b,c},{1,2,3}。如何提取得到类似

String1 = a,b,c;
String2 = 1,2,3;

我尝试过类似的方法,但不起作用。

String result1 = str.substring(str.indexOf("{") + 1, str.indexOf("},"));
String result2 = str.substring(str.indexOf(",{") + 1, str.indexOf("}"));

最佳答案

有一个 indexOf 方法,它获取要搜索的索引

String str = "{a,b,c},{1,2,3}";
int startingIndex = str.indexOf("{");
int closingIndex = str.indexOf("}");
String result1 = str.substring(startingIndex + 1, closingIndex);
System.out.println(result1);

startingIndex = str.indexOf("{", closingIndex + 1);
closingIndex = str.indexOf("}", closingIndex + 1);
String result2 = str.substring(startingIndex + 1, closingIndex);
System.out.println(result2);

在第二个 block 中,我们从 endingIndex + 1 开始搜索,其中 endingIndex 是最后一次看到的 } 的索引。

关于java - 在java中提取大括号之间的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60417383/

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