我想按字符数组拆分字符串,所以我有以下代码:
String target = "hello,any|body here?";
char[] delim = {'|',',',' '};
String regex = "(" + new String(delim).replaceAll("(.)", "\\\\$1|").replaceAll("\\|$", ")");
String[] result = target.split(regex);
一切正常,除非我想在 delim[] 数组中添加像“Q”这样的字符,它会抛出异常:
java.util.regex.PatternSyntaxException: Illegal/unsupported escape sequence near index 11
(\ |\,|\||\Q)
那么我该如何解决它以使用非特殊字符呢?
提前致谢