Background problem
The following java can say exactly after the implementation of what will print out?
System.out.println( String.class.getName()+ ".class"); System.out.println( String.class.getName(). replaceAll(".","/") + ".class");
I believe for the first row, most people do not make mistakes, print
java.lang.String.class
We want to use / to split the class package, look forward to the results print
java/lang/String/class
The results of the real return is like this:
////////////////.class
Why is this so
The problem is that String.replaceAll received a regular expression as its first argument, and
Non-acceptance of a literal sequence of characters . (Regular expressions has been added to the Java Platform 1.4
Version. ) Regular expression. "" Matches any single character, and therefore, the name of each class
Characters have been replaced with a slash, and produce the output we see.
Solutions
Method 1: Use the escape character
System.out.println( String.class.getName(). replaceAll("\\.","/") + ".class");
Print results
java/lang/String.class
Is not it a bit do not understand, why are there two?
The first "\" represents the reference (Quotation regular expressions), second code "\" escape
Quotation
\ Nothing, but quotes the following character
\Q Nothing, but quotes all characters until \E
\E Nothing, but ends quoting started by \Q
Second way to use Quotation
System.out.println( String.class.getName(). replaceAll("\\Q.\\E","/") + ".class");
The result is
java/lang/String.class
You can also use
System.out.println( String.class.getName(). replaceAll(Pattern.quote("."),"/") + ".class");
Its internal implementation is to use 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(); }
Common special characters are:
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)
and also
Twelve tokens, formed from ASCII characters, are the separators (punctuators).
( ) { } [ ] ; , . ... @ ::
It may also be determined using the following method
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"); } } } }
More detailed information can refer to the official documentation [3]
References:
[1] java doubts
【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