能准确说出下面的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 /Q Nothing, but quotes all characters until /E /E Nothing, 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 <code>String</code> for the specified * <code>String</code>. * * <p>This method produces a <code>String</code> that can be used to * create a <code>Pattern</code> that would match the string * <code>s</code> as if it were a literal pattern.</p> Metacharacters * or escape sequences in the input sequence will be given no special * meaning. * * @param s The string to be literalized * @return A literal string replacement * @since 1.5 */ public static String 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"); return sb.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).
( ) { } [ ] ; , . ... @ ::
也可以使用下面的方法进行判断
import java.util.regex.Matcher; import java.util.regex.Pattern; public class CheckSpecialCharacterString { /** * Check whether the each character of String is special character or not using java * @author www.instanceofjava.com */ public static void main(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