开发者

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


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.


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


Try this

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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜