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"?
I tried the procedures of your posts but with no success.
This is what I get from debugger:
Original string that I save into sqlite database was b\r\na
.. when I read them, I get b\\r\\na
(length in debugger is 6: "b" "\" "\r" "\" "\n" "a"
) then I try replace this string and I get string with length 6 again (you can see in picture above).
I run this short script in my test form with only one text box:
private void Form_Load(object sender, EventArgs e)
{
string x = "b\\r\\na";
string y = x.Replace(@"\\", @"\");
this.textBox.Text = y + "\r\n\r\nLength: " + y.Length.ToString();
}
and I get this in text box (so, no new line characters between "b" and "a":
b\r\na
Length: 6
What can I do with this string to unescape backslash? (I expect new line between "b" and "a".)
Solution:
OK, this is not possible to do with standard replace, because of \r
and \n
is one character. Is possible to replace part of string character by character but not possible to replace "half part" of one character. So, I must replace any special character separatelly, like this:
private void Form_Load(object sender, EventArgs e) {
...
string z = x.Replace(@"\r\n", Environment.NewLine);
...
This produce correct result for me:
b
a
Try -
var newstring = @"a\\b".Replace(@"\\",@"\");
string a = @"a\\b";
a = a.Replace(@"\\",@"\");
should work. Remember that in the watch Visual STudio show the "\" escaped so you see "\" in place of a single one.
Regex.Unescape(string) method converts any escaped characters in the input string.
The Unescape method performs one of the following two transformations:
It reverses the transformation performed by the Escape method by removing the escape character ("\") from each character escaped by the method. These include the \, *, +, ?, |, {, [, (,), ^, $, ., #, and white space characters. In addition, the Unescape method unescapes the closing bracket (]) and closing brace (}) characters.
It replaces the hexadecimal values in verbatim string literals with the actual printable characters. For example, it replaces @"\x07" with "\a", or @"\x0A" with "\n". It converts to supported escape characters such as \a, \b, \e, \n, \r, \f, \t, \v, and alphanumeric characters.
string str = @"a\\b\\c";
var output = System.Text.RegularExpressions.Regex.Unescape(str);
Reference:
I was having the same problem until I read Jon Skeet's answer about the debugger displaying a single backslash with a double backslash even though the string may have a single backslash. I was not aware of that. So I changed my code from
text2 = text1.Replace(@"\\", @"/");
to
text2 = text1.Replace(@"\", @"/");
and that solved the problem. Note: I'm interfacing and R.Net which uses single forward slashes in path strings.
in case someone got stuck with this and none of the answers above worked, below is what worked for me. Hope it helps.
var oldString = "\\r|\\n";
// None of these worked for me
// var newString = oldString(@"\\", @"\");
// var newString = oldString.Replace("\\\\", "\\");
// var newString = oldString.Replace("\\u5b89", "\u5b89");
// var newString = Regex.Replace(oldString , @"\\", @"\");
// This is what worked
var newString = Regex.Unescape(oldString);
// newString is now "\r|\n"
You can simply do a replace in your string like
Str.Replace(@"\\",@"\");
Source: Stackoverflow.com