问题背景
能准确说出下面的java 执行完毕后会打印出什么?
System.out.println(
String.class.getName()+ ".class");
System.out.println(
String.class.getName().
replaceAll(".","/") + ".class");
相信对于第一行,大部分人不会犯错,打印
java.lang.String.class
我们想使用/去分割类的包,期待打印的结果为
java/lang/String/class
真实返回的结果是这个样子的:
////////////////.class
为什么会这样呢

原因
问题在于String.replaceAll 接受了一个正则表达式作为它的第一个参数,而并
非接受了一个字符序列字面常量。(正则表达式已经被添加到了Java 平台的1.4
版本中。)正则表达式“.”可以匹配任何单个的字符,因此,类名中的每一个
字符都被替换成了一个斜杠,进而产生了我们看到的输出。
解决方式
方式一:使用转义字符
System.out.println(
String.class.getName().
replaceAll("\\.","/") + ".class");
打印结果
java/lang/String.class
是不是有点不懂,为什么会有两个?
第一个"\"代表的是引用(正则表达式中的Quotation),第二个代码"\"转义
Quotation
\Nothing, but quotes the following character
\QNothing, but quotes all characters until \E
\ENothing, but ends quoting started by \Q
方式二 使用Quotation
System.out.println(
String.class.getName().
replaceAll("\\Q.\\E","/") + ".class");
结果也是
java/lang/String.class
也可以使用
System.out.println(
String.class.getName().
replaceAll(Pattern.quote("."),"/") + ".class");
其内部实现也是使用Quotation
/*** Returns a literal pattern String for the specified
* String.
*
*
This method produces a String that can be used to
* create a Pattern that would match the string
* s as if it were a literal pattern.
* or escape sequences in the input sequence will be given no special
* meaning.
*
*@params The string to be literalized
*@returnA literal string replacement
*@since1.5*/
public staticString quote(String s) {int slashEIndex = s.indexOf("\\E");if (slashEIndex == -1)return "\\Q" + s + "\\E";
StringBuilder sb= new StringBuilder(s.length() * 2);
sb.append("\\Q");
slashEIndex= 0;int current = 0;while ((slashEIndex = s.indexOf("\\E", current)) != -1) {
sb.append(s.substring(current, slashEIndex));
current= slashEIndex + 2;
sb.append("\\E\\\\E\\Q");
}
sb.append(s.substring(current, s.length()));
sb.append("\\E");returnsb.toString();
}
常见的特殊字符有:
EscapeSequence:
\ b (backspace BS, Unicode \\u0008)
\ t (horizontal tab HT, Unicode \\u0009)
\ n (linefeed LF, Unicode \\u000a)
\ f (form feed FF, Unicode \\u000c)
\ r (carriage return CR, Unicode \\u000d)
\ " (double quote ", Unicode \\u0022)
\ ' (single quote ', Unicode \\u0027)
\ \ (backslash \, Unicode \\u005c)
OctalEscape (octal value, Unicode \\u0000 to \\u00ff)
还有
Twelve tokens, formed from ASCII characters, are the separators (punctuators).
( ) { } [ ] ; , . ... @ ::
也可以使用下面的方法进行判断
importjava.util.regex.Matcher;importjava.util.regex.Pattern;public classCheckSpecialCharacterString {/*** Check whether the each character of String is special character or not using java
*@authorwww.instanceofjava.com*/
public static voidmain(String[] args) {
String Str="Java String interview questions*$%";
String specialCharacters=" !#$%&'()*+,-./:;<=>?@[]^_`{|}";for (int i = 0; i < Str.length(); i++) {if(specialCharacters.contains(Character.toString(Str.charAt(i))))
{
System.out.println(Str.charAt(i)+": is a special character");
}
}
}
}
更详细的资料可以参考官方文档【3】
参考资料:
【1】java解惑
【2】https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
【3】https://docs.oracle.com/javase/specs/jls/se12/html/jls-3.html#jls-3.10.6
【4】http://www.instanceofjava.com/2017/05/how-to-check-if-character-is-special.html
本文探讨了在Java中使用String.replaceAll方法时,由于正则表达式的误用导致的问题。当尝试将字符串中的点号(.)替换为斜杠(/)时,错误地使用了正则表达式,结果每个字符被替换为斜杠。解决方法包括使用转义字符(\)或Pattern.quote方法。此外,文章还介绍了Java中的特殊字符和转义序列。
1069

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



