Vinay
Vinay

Reputation: 1

string replace for special character

I have string as like the following \0\0\0\0\0\0\0\0. I would like to replace the \ symbol in between the string

Could anybody tell me how I can replace or remove those \ back slash from that string.

I have used string replace with @ symbol ex: string.Replace(@"\","") & also used string.Trim('\0') and string.TrimEnd('\0')

Tell me how I can remove those special character from the symbol.

Vinay

Upvotes: 0

Views: 3132

Answers (4)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038810

If you tried s.Replace(@"\", "") and this didn't yield the expected results it means that in reality there is no \ character in your actual string. It is what you see in Visual Studio debugger. The actual string maybe contains the 0 byte. To remove it you could:

string s = Encoding.UTF8.GetString(new byte[] { 0, 0, 0, 0 });
s = s.Trim('\0');

Notice that because of the strings being immutable in .NET you need to reassign the string to the result of the Trim method as it doesn't modify the original string.

Upvotes: 3

BirgerH
BirgerH

Reputation: 748

This works for me without issues:

string s1 = @"\0\0\0\0\0\0\0\0";
string s2 = s1.Replace("\\", "");
Console.WriteLine(s2);

Output:

00000000

Upvotes: 0

Maxim Shevtsov
Maxim Shevtsov

Reputation: 212

Try this

var str=@"\0\0\0\0\0\0\0\0";
str.Replace(@"\","");

Upvotes: 0

james_bond
james_bond

Reputation: 6908

Maybe String.Replace("\\","")

Upvotes: 1

Related Questions