-1

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.

Soumya Boral
  • 1,191
  • 14
  • 28
  • 2
    No idea about R but is your input string actually valid using multiple double quotes inside? Aren't the backslashes there for a reason and escape the quotes inside the string? I seen a post stating `cat(test)` would print your value without the escape characters. – JvdV Mar 09 '20 at 21:38
  • It is actually created by appending strings dynamically using paste0(). Hence it looks like this. – Soumya Boral Mar 09 '20 at 21:42
  • cat() just escapes those characters and print the unescaped version as expected. That don't help much. – Soumya Boral Mar 09 '20 at 21:44
  • 1
    Well, It was just an suggestion. For me, not knowing much about R it just looks like you would corrupt your string removing those backslashes. Maybe that's why R won't let you? Maybe [this](https://campus.datacamp.com/courses/string-manipulation-with-stringr-in-r/string-basics?ex=2) is helpfull and you should change the outer double quotes to single quotes? Good luck anyways =) – JvdV Mar 09 '20 at 21:50
  • @JvdV is correct - you can't remove the backslashes because they don't exist in your data - they're there to escape the quotes and are an artefact of printing. You can see that using `nchar(test)` returns `5` not `7`. – Ritchie Sacramento Mar 09 '20 at 22:08
  • Your third character is \" it isn't a \. – André Oliveira Mar 09 '20 at 22:11
  • Have added the main context in question as Edit for better clarification of the source of single backslash in R. – Soumya Boral Mar 10 '20 at 08:01
  • Please reopen the question. I have clearly mentioned in the question itself that none of the previous solutions given in other links work in my case. – Soumya Boral Mar 10 '20 at 08:06

2 Answers2

0

There is no backslash in your string

grepl("\\", test, fixed = TRUE)
# FALSE
asdaa
  • 11
  • 2
0

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.

torpzorrr
  • 75
  • 5