2015-04-04 98 views
0

我有这个字符串。用两个不同的分隔符分割字符串

[email protected] 

我想用indexOf('@')找到它找到了三个不同的部分(忽略@

过去,我不知道下一步该怎么做。 像indexOf()还有什么其他的东西可以使用?

+1

你要分割非字字符的文本?然后看看'split'方法。 – 2015-04-04 03:02:28

回答

0

像indexOf()可以使用的其他东西?

你需要indexOf

String text = "[email protected]"; 
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);