更新時間:2022-09-23 10:30:28 來源:動力節(jié)點 瀏覽1734次
因為字符串必須寫在引號內(nèi),Java 會誤解這個字符串,并產(chǎn)生錯誤:
String txt = "We are the so-called "Vikings" from the north.";
避免這個問題的解決方案是使用反斜杠轉(zhuǎn)義字符。
反斜杠 ( \) 轉(zhuǎn)義字符將特殊字符轉(zhuǎn)換為字符串字符:
| Escape character | Result | Description |
|---|---|---|
| \' | ' | Single quote |
| \" | " | Double quote |
| \\ | \ | Backslash |
該序列\(zhòng)" 在字符串中插入雙引號:
String txt = "We are the so-called \"Vikings\" from the north.";
該序列\(zhòng)' 在字符串中插入一個單引號:
String txt = "It\'s alright.";
該序列\(zhòng)\ 在字符串中插入一個反斜杠:
String txt = "The character \\ is called backslash.";