Even more convincing than nchar is cat vs. print:
s <- "<span id=\"ctl00"
cat( s )
print( s )
The print function formats character strings in a manner that allows you to copy
and paste them into source files. The cat function just dumps the contents of
the string out to the console, which can trigger the control behaviors of the
terminal (moving cursor around, clearing the screen, beeping, etc.) but without
the escapes needed to encode those characters in into source code.
Note that the whole reason R uses two kinds of quotes for character literals is
to allow you to use fewer escape slashes:
s <- '<span id="ctl00'
A double quote inside a single-quote-delimited string is an ordinary character
because there is no possible confusion that it might be the end of the string.
BTW the str symbol is a commonly-used function from base R... it is not a good
idea to use it as a throwaway variable.
On September 21, 2018 3:48:55 AM PDT, Rui Barradas <ruipbarradas at
sapo.pt> wrote:>Hello,
>
>The backslash is not a character of the string str. test it with nchar:
>
>str = '<span id=\"ctl00'
>nchar(str)
>#[1] 15
>
>
>It is there just to escape the double quotes.
>if you want a backslash you would need to escape it, like in str1.
>
>
>str1 = '<span id=\\"ctl00'
>nchar(str1)
>#[1] 16
>
>
>Now yes, you can remove it.
>
>str2 <- gsub("\\", "", str1, fixed = TRUE)
>identical(str, str2)
>#[1] TRUE
>
>
>
>Hope this helps,
>
>Rui Barradas
>
>?s 11:34 de 21/09/2018, Christofer Bogaso escreveu:
>> Hi,
>>
>> I have below string where I am trying to remove Backslash from. I
>tried
>> with gsub() function, however failed to remove that:
>>
>>> str = '<span id=\"ctl00'
>>> gsub("\\", "", str)
>> Error in gsub("\\", "", str) :
>> invalid regular expression '\', reason 'Trailing
backslash'
>>
>>
>> Any pointer to the right approach?
>>
>> Thanks for your time
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>>
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>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.
--
Sent from my phone. Please excuse my brevity.