Skip to content

How to get the string '\'?

6 messages · Peng Yu, Linlin Yan, David Winsemius

#
I can not get the string '\'. Could somebody let me know how to get it?
+
+
[1] "\\"
#
?cat

 > cat("\\")
\
On Nov 15, 2009, at 5:30 PM, Peng Yu wrote:

            
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
#
My question was from replacing a pattern by '\\'. How to replace '/'
in string by '\'?

string='abc/efg'
gsub('/','\\',string)
On Sun, Nov 15, 2009 at 5:07 PM, David Winsemius <dwinsemius at comcast.net> wrote:
#
Regular expression needs double the '\' again, so try this:
gsub('/','\\\\',string)
On Mon, Nov 16, 2009 at 7:35 AM, Peng Yu <pengyu.ut at gmail.com> wrote:
#
On Nov 15, 2009, at 6:35 PM, Peng Yu wrote:

            
No,  that was most definitely _not_ your posed question. If you want  
now to change your question and supply a reproducible example, that's  
fine, just don't claim that your mind should have been read more  
properly that it was, .... please.

The problem with your _second_ question is that the printed  
representation of "\" is a problem because of its special use as an  
escape symbol. So sometimes it needs to be displayed as "\\". What  
gets written to the screen may be different that the internal  
representation. Look at the results of:
 > string='abc/efg'
 > cat(gsub('/','\\\\',string), file="test.txt")

You should see:
abc\efg

...although at the screen you would see:

 > string='abc/efg'
 > gsub('/','\\\\',string)
[1] "abc\\efg"

The first "\" escapes second "\" which in turn allows whatever follows  
to be interpreted as "escaped", while the third "\" escapes the 4th  
"\" so that it can be examined by the R interpreter as a real "\".
#
On Sun, Nov 15, 2009 at 6:05 PM, David Winsemius <dwinsemius at comcast.net> wrote:
Sorry for the misunderstanding. I realized that the answer to the
first question could not solve my original question (but I thought it
could). So I stated my original question.