I’m writing strings which contain backslashes (\) to a file:
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:
\str
\\str
If I want to get a string containing 5 backslashes, should I write 10 backslashes, like this?
x = "\\\\\\\\\\str"
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\nas newline.) It’s also useful if you want to write a string containing a single". You write it as"\"".The reason why
\\\stris invalid, is because it’s interpreted as\\(which corresponds to a single\) followed by\s, which is not valid, since “escapeds” has no meaning.