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 | 退格 |