java中最容易犯錯的特殊字元
問題背景
能準確說出下面的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.