我在搜索特殊字符“(”时遇到了一些问题。
我得到一个 java.util.regex.PatternSyntaxException exception has occurred
。
这可能与“(”被视为特殊字符有关。
我不太擅长模式表达。有人可以帮我正确搜索转义字符吗?
// I need to split the string at the "("
String myString = "Room Temperature (C)";
String splitList[] = myString.split ("("); // i got an exception
// I tried this but got compile error
String splitList[] = myString.split ("\(");
最佳答案
尝试其中之一:
string.split("\\(");
string.split(Pattern.quote("("));
由于字符串拆分采用正则表达式作为参数,因此您需要正确转义字符。参见 Jon Skeet 的 answer在这里:
关于java - 如何在java字符串中搜索特殊字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16679197/