David Winsemius wrote:

On Dec 13, 2009, at 1:00 PM, Sean Zhang wrote:

Dear R-helpers.

Can someone kindly tell me how to replace a single forward slash with double
backward slash in a string?

i.e.,  from  "a/b" to "a\\b"

 > sub("/", "\\\\","a/b")
 #the backslashes need to be doubled because they are the escape character.
[1] "a\\b"

Bzzzt. Wrong answer.

> nchar(sub("/", "\\\\","a/b") )
[1] 3
> cat(sub("/", "\\\\","a/b"),"\n")
a\b

> nchar(sub("/", "\\\\\\\\","a/b") )
[1] 4
> sub("/", "\\\\\\\\","a/b")
[1] "a\\\\b"
> cat(sub("/", "\\\\\\\\","a/b"),"\n")
a\\b

You are not the first... ;-)

--
   O__  ---- Peter Dalgaard             Ă˜ster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalga...@biostat.ku.dk)              FAX: (+45) 35327907

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to