I'm writing strings to a file using R:
我用R将字符串写入文件:
> x1="\\str"> x2="\\\str"Error: '\s' is an unrecognized escape in character string starting "\\\s"> x2="\\\\str"> write(file='test',c(x1,x2))
When I open the file named test
, I see this:
当我打开名为test的文件时,我看到:
\str\\str
If I want to get a string containing 5 backslashes, should I write 10 backslashes, like this?
如果我想要一个包含5个反斜杠的字符串,我应该写10个反斜杠,像这样吗?
x="\\\\\\\\\\str"
3 个解决方案
#1
16
[...] If I want to get a string containing 5
\
,should i write 10\
[...][…[英语背诵文选如果我想要一个包含5只\的字符串,我应该写10只\[…]
Yes, you should. To write a single \
in a string, you write it as "\\"
.
是的,你应该。要在一个字符串中写一个\,你可以把它写成“\”。
This is because the \
is a special character, reserved to escape the character that follows it. (Perhaps you recognize \n
as newline.) It's also useful if you want to write a string containing a single "
. You write it as "\""
.
这是因为\是一个特殊的字符,保留以逃避跟随它的字符。(也许你认识\n是换行符。)如果您想编写一个包含单个“”的字符串,那么它也很有用。你把它写成“\”。
The reason why \\\str
is invalid, is because it's interpreted as \\
(which corresponds to a single \
) followed by \s
, which is not valid, since "escaped s
" has no meaning.
\str是无效的原因,是因为它被解释为\(对应于单个\),然后是s,这是无效的,因为“转义s”没有意义。
#2
6
Note that the doubling of backslashes is because you are entering the string at the command line and the string is first parsed by the R parser. You can enter strings in different ways, some of which don't need the doubling. For example:
请注意,反斜杠的加倍是因为您在命令行中输入了字符串,并且字符串首先由R解析器解析。您可以以不同的方式输入字符串,其中一些不需要加倍。例如:
> tmp <- scan(what='')1: \\\\\str2: Read 1 item> print(tmp)[1] "\\\\\\\\\\str"> cat(tmp, '\n')\\\\\str >
#3
6
Have a read of this section about character vectors.
请阅读本节有关字符向量的部分。
In essence, it says that when you enter character string literals you enclose them in a pair of quotes (" or '). Inside those quotes, you can create special characters using \ as an escape character.
从本质上说,它说当您输入字符字符串时,您将它们用一对引号括起来(“或”)。在这些引号中,您可以使用\作为转义字符来创建特殊字符。
For example, \n denotes new line or \" can be used to enter a " without R thinking it's the end of the string. Since \ is an escape character, you need a way to enter an actual . This is done by using \\. Escaping the escape!
例如,\n表示新的行或\“可以用来输入”,而R不认为它是字符串的末尾。因为\是转义字符,所以您需要一种输入实际字符的方式。这是用\\ \来做的。逃离逃离!
#1
16
[...] If I want to get a string containing 5
\
,should i write 10\
[...][…[英语背诵文选如果我想要一个包含5只\的字符串,我应该写10只\[…]
Yes, you should. To write a single \
in a string, you write it as "\\"
.
是的,你应该。要在一个字符串中写一个\,你可以把它写成“\”。
This is because the \
is a special character, reserved to escape the character that follows it. (Perhaps you recognize \n
as newline.) It's also useful if you want to write a string containing a single "
. You write it as "\""
.
这是因为\是一个特殊的字符,保留以逃避跟随它的字符。(也许你认识\n是换行符。)如果您想编写一个包含单个“”的字符串,那么它也很有用。你把它写成“\”。
The reason why \\\str
is invalid, is because it's interpreted as \\
(which corresponds to a single \
) followed by \s
, which is not valid, since "escaped s
" has no meaning.
\str是无效的原因,是因为它被解释为\(对应于单个\),然后是s,这是无效的,因为“转义s”没有意义。
#2
6
Note that the doubling of backslashes is because you are entering the string at the command line and the string is first parsed by the R parser. You can enter strings in different ways, some of which don't need the doubling. For example:
请注意,反斜杠的加倍是因为您在命令行中输入了字符串,并且字符串首先由R解析器解析。您可以以不同的方式输入字符串,其中一些不需要加倍。例如:
> tmp <- scan(what='')1: \\\\\str2: Read 1 item> print(tmp)[1] "\\\\\\\\\\str"> cat(tmp, '\n')\\\\\str >
#3
6
Have a read of this section about character vectors.
请阅读本节有关字符向量的部分。
In essence, it says that when you enter character string literals you enclose them in a pair of quotes (" or '). Inside those quotes, you can create special characters using \ as an escape character.
从本质上说,它说当您输入字符字符串时,您将它们用一对引号括起来(“或”)。在这些引号中,您可以使用\作为转义字符来创建特殊字符。
For example, \n denotes new line or \" can be used to enter a " without R thinking it's the end of the string. Since \ is an escape character, you need a way to enter an actual . This is done by using \\. Escaping the escape!
例如,\n表示新的行或\“可以用来输入”,而R不认为它是字符串的末尾。因为\是转义字符,所以您需要一种输入实际字符的方式。这是用\\ \来做的。逃离逃离!