在使用String.split方法分隔字符串时,分隔符如果用到一些特殊字符,可能会得不到我们预期的结果。
因此要在特殊字符前加\\
public String getUserAccount()
{
String[] temp1 = userAccount.split("\\(");
String[] temp2 = temp1[1].split("\\)");
return temp2[0];
}
该方法也可以如下实现:
public String getUserAccount()
{
String temp1 = userAccount.substring(userAccount.indexOf("(") + 1,
userAccount.length() - 1);
return temp1;
}
本文提供了一种方法,在使用String.split处理包含特殊字符的字符串时,通过在特殊字符前添加转义符来避免预期外的结果。同时,还提供了一种替代方法,使用substring来直接截取需要的部分。
1356

被折叠的 条评论
为什么被折叠?



