更新時間:2022-09-23 10:30:28 來源:動力節點 瀏覽1321次
因為字符串必須寫在引號內,Java 會誤解這個字符串,并產生錯誤:
String txt = "We are the so-called "Vikings" from the north.";
避免這個問題的解決方案是使用反斜杠轉義字符。
反斜杠 ( \) 轉義字符將特殊字符轉換為字符串字符:
Escape character | Result | Description |
---|---|---|
\' | ' | Single quote |
\" | " | Double quote |
\\ | \ | Backslash |
該序列\" 在字符串中插入雙引號:
String txt = "We are the so-called \"Vikings\" from the north.";
該序列\' 在字符串中插入一個單引號:
String txt = "It\'s alright.";
該序列\\ 在字符串中插入一個反斜杠:
String txt = "The character \\ is called backslash.";
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習