Java 用两个不同的分隔符拆分字符串

Java 用两个不同的分隔符拆分字符串,java,string,Java,String,我有这根绳子 One@two.three 我想找出三个不同的部分@ 过去使用indexOf'@'查找它,我不知道下一步该怎么做。 我还可以使用indexOf之类的东西吗 我还可以使用indexOf之类的东西吗 你需要索引 使用: : 是否要按非单词字符拆分文本?然后看一下split方法。 String text = "One@two.three"; int pos1 = text.indexOf('@'); // search for the first `.` after the `@

我有这根绳子

One@two.three
我想找出三个不同的部分@

过去使用indexOf'@'查找它,我不知道下一步该怎么做。 我还可以使用indexOf之类的东西吗

我还可以使用indexOf之类的东西吗

你需要索引

使用:

:


是否要按非单词字符拆分文本?然后看一下split方法。
 String text = "One@two.three";
 int pos1 = text.indexOf('@');
 // search for the first `.` after the `@`
 int pos2 = text.indexOf('.', pos1 + 1);

 if (pos1 < 0 || pos2 < 0)
    throw new IllegalArgumentException();


 String s1 = text.substring(0, pos1);
 String s2 = text.substring(pos1 + 1, pos2);
 String s3 = text.substring(pos2 + 1);
final String input = "One@two.three";
for (String field: input.split("@|\\.")) {
    System.out.println(field);  
}
One
two
three