R 跳脫字元
跳脫字元
要在字串中插入非法字元,您必須使用跳脫字元。
跳脫字元是一個反斜槓 \
後跟你想插入的字元。
非法字元的一個例子是,在由雙引號括起來的字串中插入雙引號。
示例
str <- "We are the so-called "Vikings", from the north."
str
結果
Error: unexpected symbol in "str <- "We are the so-called "Vikings"
要解決這個問題,請使用跳脫字元 \"
。
示例
跳脫字元允許你在正常情況下不允許的情況下使用雙引號。
str <- "We are the so-called \"Vikings\", from the north."
str
cat(str)
自己動手試一試 »
請注意,自動列印 str 變數時會顯示反斜槓。您可以使用 cat()
函式將其打印出來而不顯示反斜槓。
R 中的其他跳脫字元
程式碼 | 結果 |
---|---|
\\ | 反斜槓 |
\n | 換行 |
\r | 回車 |
\t | 製表符 |
\b | 退格 |