我有一个字符串,需要将其拆分为 2 个。我想通过在第三个逗号处拆分来完成此操作。
我该怎么做?
编辑
示例字符串是:
from:09/26/2011,type:all,to:09/26/2011,field1:emp_id,option1:=,text:1234
字符串将保持相同的格式 - 我希望字段之前的所有内容都在字符串中。
最佳答案
如果您只是想在第三个逗号的索引处拆分字符串,我可能会这样做:
String s = "from:09/26/2011,type:all,to:09/26/2011,field1:emp_id,option1:=,text:1234";
int i = s.indexOf(',', 1 + s.indexOf(',', 1 + s.indexOf(',')));
String firstPart = s.substring(0, i);
String secondPart = s.substring(i+1);
System.out.println(firstPart);
System.out.println(secondPart);
输出:
from:09/26/2011,type:all,to:09/26/2011
field1:emp_id,option1:=,text:1234
<小时/>
相关问题:
关于Java 在第三个逗号上分割字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7567183/