String Replace a Backslash

String replace a Backslash

sSource = sSource.replace("\\/", "/");
  • String is immutable - each method you invoke on it does not change its state. It returns a new instance holding the new state instead. So you have to assign the new value to a variable (it can be the same variable)
  • replaceAll(..) uses regex. You don't need that.

String.replaceAll single backslashes with double backslashes

The String#replaceAll() interprets the argument as a regular expression. The \ is an escape character in both String and regex. You need to double-escape it for regex:

string.replaceAll("\\\\", "\\\\\\\\");

But you don't necessarily need regex for this, simply because you want an exact character-by-character replacement and you don't need patterns here. So String#replace() should suffice:

string.replace("\\", "\\\\");

Update: as per the comments, you appear to want to use the string in JavaScript context. You'd perhaps better use StringEscapeUtils#escapeEcmaScript() instead to cover more characters.

how to replace back slash character with empty string in python

We need to specify that we want to replace a string that contains a single backslash. We cannot write that as "\", because the backslash is escaping the intended closing double-quote. We also cannot use a raw string literal for this: r"\" does not work.

Instead, we simply escape the backslash using another backslash:

result = string.replace("\\","")

Replacing backslash in a string

string.Replace does not modify the string itself but returns a new string, which most likely you are throwing away. Do this instead:

myString= myString.Replace(@"\","-");

On a side note, this kind of operation is usually seen in code that manually mucks around with formatted date strings. Most of the time there is a better way to do what you want (which is?) than things like this.

How to replace Backslash '\' with double Backslash in Dart?

The problem is that the string string does not contain any \

It would either need to be

String string = r"back\slash back\slash back\slash back\slash";

or

String string = "back\\slash back\\slash back\\slash back\\slash";

In your example there also is no need for RegExp.
Just

String replaced = string.replaceAll(r'\', r'\\');

would do as well.

Replace back slash (\) with forward slash (/)

You have to escape to backslashes.

var path = "C:\\test1\\test2";var path2 = path.replace(/\\/g, "/");console.log(path2);

replace backslash in redshift

REPLACE( string, '\\"', '"' ) seems works in this situation. I am guessing its because redshift doesn't allow a single backslash, but converts them to double backslash.

So, even though the string looked like \"name\" it was probably stored as \\"name\\" and hence putting a single backslash in the replace was not working.

EDIT: please read Bill's explanation in the comment below this reply

String replace character with backslash and double quote in a column of an R dataframe

I think in R, they will always print two backslashes together if one of them are escaped. When two backslashes are shown together, it is only a syntax to show that these should be interpreted as a character "\" but not an escape character.

To confirm that, you can try to save your dataframe to a text file, you will see that there's actually only one backslash in the string.

df <- df %>% mutate(option_json = gsub(" inches", '\\\\"', option_json, ignore.case = T) %>% 
gsub(" Feet", "\\'", ., ignore.case = T))

write.table(df, "df.tsv", quote = F, row.names = F)

Output copied from "df.tsv"

ID option_json
1 {"thickness":"0.031\"","tensile strength":"600 lb","size":"0.5\" x 7200'"}
2 {"thickness":"0.031\"","tensile strength":"600 lb","size":"0.5\" x 7200'"}
3 {"tensile strength":"600 lb","color":"Black","size":"0.5\" x 7200'"}

Try printing the "option_json" column

You can see that before every double quote " character, there is a escape character \. And \\ is used to indicate a single \ character.

print(df$option_json)
[1] "{\"thickness\":\"0.031\\\"\",\"tensile strength\":\"600 lb\",\"size\":\"0.5\\\" x 7200'\"}"
[2] "{\"thickness\":\"0.031\\\"\",\"tensile strength\":\"600 lb\",\"size\":\"0.5\\\" x 7200'\"}"
[3] "{\"tensile strength\":\"600 lb\",\"color\":\"Black\",\"size\":\"0.5\\\" x 7200'\"}"

How to replace backslashes to a different character in a string Python

You need to escape the backslash -

temp = r"abc\abc"
temp.replace('\\', 'backslash')
'abcbackslashabc'


Related Topics



Leave a reply



Submit