Remove Backslashes from Character String

Remove backslashes from character string

Perhaps you would like to see a different representation of the same string:

test2 <- 'select case when "est"  dsaf'
test<- "select case when \"est\" dsaf"
identical(test, test2)
#[1] TRUE

When a character value is built with double quotes, any interior instances of \" become only double-quotes. They will be displayed by print (and by the REPL that you see in an interactive session) with the escape-backslash, but using cat you cant determine that they are not really in there as backslashes.

Further proof:

>  nchar("\"")
[1] 1

You can use either cat or print with quote=FALSE in you want to display the value as it really exists internally:

> print(test, quote=FALSE)
[1] select case when "est" dsaf

This is evidence that at least one version of "SQL" agrees (or "accepts") that there is no backslash when \" appears in the interior of a string:

> require(sqldf)
Loading required package: sqldf
Loading required package: gsubfn
Loading required package: proto
Loading required package: RSQLite
Loading required package: DBI
> ?sqldf
> a1r <- head(warpbreaks)
> a1s <- sqldf("select * from warpbreaks limit 6")
Loading required package: tcltk
> a2s <- sqldf("select * from CO2 where Plant like 'Qn%'")
>
> a2sdq <- sqldf("select * from CO2 where Plant like \"Qn%\"")
> identical(a2s,a2sdq)
[1] TRUE

So the was the first problem. The second problem was trying to assign the value of a cat call. The cat function always returns NULL after sending its value to a destination, possibly the console output. You cannot save the resulting character value to an R name. You always get NULL. See the ?cat help page.

Remove Backslash and Quotations in R

You don't need to escape anything if you don't use a regex:

gsub('\"', "", test, fixed = TRUE)
#[1] "LAST4"

How do you remove backslashes and the word attached to the backslash in Python?

Python is somewhat hard to convince to just ignore unicode characters. Here is a somewhat hacky attempt:

l = ['Historical Notes 1996',
'\ue606',
'The Future of farms 2012',
'\ch889',
'\8uuuu',]


def not_unicode_or_backslash(x):
try:
x = x.encode('unicode-escape').decode()
finally:
return not x.startswith("\\")


[x for x in l if not_unicode_or_backslash(x)]

# Output: ['Historical Notes 1996', 'The Future of farms 2012']

The problem is that you can't check directly whether or not the string starts with a backslash since \ue606 is not considered as the 6-character string, but as a single unicode character. Because of this, it does not start with a backslash and for

[x for x in l if not x.startswith("\\")]

you get

['Historical Notes 1996', '\ue606', 'The Future of farms 2012']

How to remove backslash from string in Python?

This is due to \ being the escape sequence character in python so you have to do \\

str2 = str1.replace('\\', '')  

Removing backslashes from string

a = "I don\'t know why I don\'t have the right answer"
b = a.strip("/")
print b
  1. Slash (/) and backslash (\) are not the same character. There are no slashes anywhere in the string, so what you're doing will have no effect.
  2. Even if you used a backslash, there are no backslashes in a anyway; \' in a non-raw string literal is just a ' character.
  3. strip only removes "the leading and trailing characters". Since you're trying to remove a character in the middle of the string, it won't help.

And maybe some meta-problems:

  1. The OP's example string actually had just backslashes, not backslash-escaped apostrophes, even though his question title said the latter. So you were solving a different problem. Which may be more the OP's fault than yours, but it's still not a good answer.
  2. The OP's code already did what you were trying to do. He obviously had some other problem that he couldn't explain. (Given that he stopped answering comments and deleted his question, my first bet would be that it was a silly typo somewhere…) So, even if you wrote your code correctly, it wouldn't have been that helpful.

At any rate, that's not too many to count.

Then again, the comment didn't say it was too many to count, just hard to count. Some people have trouble counting to four three. Even Kings of the Britons have to be reminded by their clerics how to do it.

Remove backslashes in string

Try using something like:

String[] names = request.getParameterValues("Name");
StringBuilder name = new StringBuilder("(");
for(int index = 0; index <names.length; index++){
name.append("'");
name.append(names[index].replace("\\","").replace("/",""));
name.append("'");
name.append(index != names.length -1? "," : ")");
}
String output = name.toString();

The replace() method replaces every instance of the sub string, therefore you do not have to use "\\\\" unless of course if you only want to remove the double slashes and leave the single slashes.

If the problem persists then there are two possible reasons for it.

  1. The debugger expresses ' as \', so there should be no problem when sending the query to the server.
  2. The \ is not actually a slash or backslash but another character that looks like as backslash. You can find which character it is by using int test = output.charAt(output.length() - 3); and then check the value of the test variable using the debugger.

how to remove backslash from string in python

You need to escape the backslash as \\.

filename = input("copy the file path with it's name and extension and paste it here to Encrypt: ")
# say it's something like "c:\myfiles\test.txt"
filename_replace = filename.replace("\\"," ")
# becomes "c: myfiles test.txt"

You can read more about escape characters and string literals here:
https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals

How to remove the backslash in string using regex in Java?

str = str.replaceAll("\\\\", "");

or

str = str.replace("\\", "");

replaceAll() treats the first argument as a regex, so you have to double escape the backslash. replace() treats it as a literal string, so you only have to escape it once.



Related Topics



Leave a reply



Submit