Peng Peng
Peng Peng

Reputation: 1365

to use escape character rightly

i found it is difficult to create character : " is entered as \ "
when i input in R:

" is entered as \"

+

" is entered as \ "

[1] " is entered as "

>" is entered as \\ " 

[1] " is entered as \ "

>" is entered as \\"    

[1] " is entered as \"

how can i get character " is entered as \ "?


i am still confused

cat("is entered as \" )

is entered as >

> "is entered as \\"

[1] "is entered as \"

> print ("is entered as \\")

[1] "is entered as \"

Upvotes: 1

Views: 157

Answers (2)

Marius
Marius

Reputation: 60070

Is this what you were trying to achieve?:

> x <- "START \" is entered as \\\" END"
> cat(x)

Gives:

START " is entered as \" END

You have to escape both the double quote ", and the backslash \, in order to have them display normally.

To clear up any confusion between whether the double quotes printed in the output are part of the string, or just symbols that wrap around the string, I've added START at the start of the string and END at the end.

Upvotes: 2

kohske
kohske

Reputation: 66842

"hoge \\" is actually hoge \

print shows \ as \\, so you find \\ for \.

try cat:

> cat("is entered as \\" )
is entered as \

and probably nchar will manifest this:

> nchar("\\" )
[1] 1

Upvotes: 3

Related Questions