更新時(shí)間:2022-09-23 10:30:28 來源:動(dòng)力節(jié)點(diǎn) 瀏覽1523次
因?yàn)樽址仨殞懺谝?hào)內(nèi),Java 會(huì)誤解這個(gè)字符串,并產(chǎn)生錯(cuò)誤:
String txt = "We are the so-called "Vikings" from the north.";
避免這個(gè)問題的解決方案是使用反斜杠轉(zhuǎn)義字符。
反斜杠 ( \) 轉(zhuǎn)義字符將特殊字符轉(zhuǎn)換為字符串字符:
Escape character | Result | Description |
---|---|---|
\' | ' | Single quote |
\" | " | Double quote |
\\ | \ | Backslash |
該序列\(zhòng)" 在字符串中插入雙引號(hào):
String txt = "We are the so-called \"Vikings\" from the north.";
該序列\(zhòng)' 在字符串中插入一個(gè)單引號(hào):
String txt = "It\'s alright.";
該序列\(zhòng)\ 在字符串中插入一個(gè)反斜杠:
String txt = "The character \\ is called backslash.";
相關(guān)閱讀
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
有基礎(chǔ) 直達(dá)就業(yè)
業(yè)余時(shí)間 高薪轉(zhuǎn)行
工作1~3年,加薪神器
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問老師會(huì)電話與您溝通安排學(xué)習(xí)