Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace "\\" with "\" in a string in C#

I still don't get how to do this. I saw many posts regarding this, but none of the solutions worked for me.

I have a string called "a\\b". The result I need is "a\b". How is this done?

I have a text file which has a database connection string pointing to an instance called - Server\DbInstance

My aim is to do a string replace in the text file -- replace "Server\DbInstance" with another value, say "10.11.12.13, 1200".

So I have:

stringToBeReplaced = @"Server\DbInstance"; newString = @"10.11.12.13, 1200"; 

This is where the problem starts. My stringToBeReplaced will always be "Server\\DbInstance", and when I search for this string in my text file, the search fails, as the text file doesn't have a string "Server\\DbInstance"; instead it has only "Server\DbInstance". So how do change "Server\\DbInstance" to "Server\DbInstance"?

like image 755
Sandeep Avatar asked Sep 20 '11 08:09

Sandeep


People also ask

How do you find and replace a word in a string in C?

The fastest way would be to allocate a new string that is strlen (s) - strlen (word) + strlen (rpwrd) + 1 . Then use the strstr function to find the word to be replaced and copy up to that point into a new string, append the new word, then copy the rest of the original sentence into a new string.

How do you replace an item in a string?

The replace() method can take maximum of 3 parameters: old - old substring you want to replace. new - new substring which will replace the old substring. count (optional) - the number of times you want to replace the old substring with the new substring.

How do I replace a character in a string in another?

The Java string replace() method will replace a character or substring with another character or string. The syntax for the replace() method is string_name. replace(old_string, new_string) with old_string being the substring you'd like to replace and new_string being the substring that will take its place.

Which function is used to replacing pattern in string?

The REGEXREPLACE( ) function uses a regular expression to find matching patterns in data, and replaces any matching values with a new string.


1 Answers

I suspect your string already actually only contains a single backslash, but you're looking at it in the debugger which is escaping it for you into a form which would be valid as a regular string literal in C#.

If print it out in the console, or in a message box, does it show with two backslashes or one?

If you actually want to replace a double backslash with a single one, it's easy to do so:

text = text.Replace(@"\\", @"\"); 

... but my guess is that the original doesn't contain a double backslash anyway. If this doesn't help, please give more details.

EDIT: In response to the edited question, your stringToBeReplaced only has a single backslash in. Really. Wherever you're seeing two backslashes, that viewer is escaping it. The string itself doesn't have two backslashes. Examine stringToBeReplaced.Length and count the characters.

like image 156
Jon Skeet Avatar answered Oct 06 '22 07:10

Jon Skeet