I have a string with backslash and I want to remove them.
test = "m \"#\""
Have tried the following but none works :
gsub( "\\\\", "", test )
gsub( "\\\\", "", test, fixed = T )
gsub( "\\", "", test )
gsub( "\\", "", test, fixed = T )
Have looked into similar questions but none of the solutions work.
Replace single backslash in R Remove Single Backslash String R
Edit : Actually this text is going to be passed in system() function to run a mosquitto client. User will give various parameters as input and the command will be created up on the fly.
The full command looks like this : mosquitto_sub -h test.mosquitto.org -q 0 -k 60 -t \\"#\\"
However it is expected to be like this : mosquitto_sub -h test.mosquitto.org -q 0 -k 60 -t "#"
Otherwise system()
does not take it. Hence is the requirement to remove the backslaches.
The parameters 0
and 60
and #
are supplied by user. Hence using paste0() to make this string. After the string is created the backslashes comes up.
The string given in text here is to create a reproducible and short example here.
There is no backslash in your string
grepl("\\", test, fixed = TRUE)
# FALSE
I think JvdV is right, I don't think the string "m "#""
can exist within R without the backslashes. The backslash makes " and # characters rather than acting as open/close quotation marks and a comment mark respectively. If you had """"
you'd get an error as you have a set of quotation marks within another set which is not possible. The same might occur for "m "#""
. However if you input "m "#""
the hash acts a comment symbol, making everything after it a comment and you get "m "
. You need backslashes to make " not a quotation mark but a character, and # not a comment symbol but a character.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.