日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

java中最容易犯错的特殊字符

發(fā)布時(shí)間:2025/4/5 编程问答 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java中最容易犯错的特殊字符 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

問(wèn)題背景

能準(zhǔn)確說(shuō)出下面的java 執(zhí)行完畢后會(huì)打印出什么?

System.out.println(String.class.getName()+ ".class");System.out.println(String.class.getName().replaceAll(".","/") + ".class");

相信對(duì)于第一行,大部分人不會(huì)犯錯(cuò),打印

java.lang.String.class

我們想使用/去分割類(lèi)的包,期待打印的結(jié)果為

java/lang/String/class

真實(shí)返回的結(jié)果是這個(gè)樣子的:

.class

為什么會(huì)這樣呢

?原因

  問(wèn)題在于String.replaceAll 接受了一個(gè)正則表達(dá)式作為它的第一個(gè)參數(shù),而并

非接受了一個(gè)字符序列字面常量。(正則表達(dá)式已經(jīng)被添加到了Java 平臺(tái)的1.4

版本中。)正則表達(dá)式“.”可以匹配任何單個(gè)的字符,因此,類(lèi)名中的每一個(gè)

字符都被替換成了一個(gè)斜杠,進(jìn)而產(chǎn)生了我們看到的輸出。

解決方式

方式一:使用轉(zhuǎn)義字符

System.out.println(String.class.getName().replaceAll("\\.","/") + ".class");

?

打印結(jié)果

java/lang/String.class

是不是有點(diǎn)不懂,為什么會(huì)有兩個(gè)?

第一個(gè)"\"代表的是引用(正則表達(dá)式中的Quotation),第二個(gè)代碼"\"轉(zhuǎn)義

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");

結(jié)果也是

java/lang/String.class

也可以使用

System.out.println(String.class.getName().replaceAll(Pattern.quote("."),"/") + ".class");

其內(nèi)部實(shí)現(xiàn)也是使用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();}

?

常見(jiàn)的特殊字符有:

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).

( ) { } [ ] ; , . ... @ ::

也可以使用下面的方法進(jìn)行判斷

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");} }}}

?

更詳細(xì)的資料可以參考官方文檔【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

轉(zhuǎn)載于:https://www.cnblogs.com/davidwang456/p/11511209.html

總結(jié)

以上是生活随笔為你收集整理的java中最容易犯错的特殊字符的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。