[R] How to remove backslash
jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Fri Sep 21 16:57:06 CEST 2018
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 using sapo.pt> wrote:
>The backslash is not a character of the string str. test it with nchar:
>str = '<span id=\"ctl00'
>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'
>Now yes, you can remove it.
>str2 <- gsub("\\", "", str1, fixed = TRUE)
>Hope this helps,
>Às 11:34 de 21/09/2018, Christofer Bogaso escreveu:
>> I have below string where I am trying to remove Backslash from. I
>> 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 using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> PLEASE do read the posting guide
>> and provide commented, minimal, self-contained, reproducible code.
>R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>PLEASE do read the posting guide
>and provide commented, minimal, self-contained, reproducible code.
Sent from my phone. Please excuse my brevity.
More information about the R-help