如何从字符串中删除“u0006”?(Replace()方法似乎没有替换任何东西)
c#
我想这是一个非常奇怪的问题:
我在 C# 中工作,我有一个字符串,只包含一个字符,ASCII 码 6。
您可以在观察窗口中看到这一点:
current "u0006" string
现在我有这段代码:
if (current.IndexOf("u0006") != -1)
current.Replace("u0006",string.Empty);
if (current != "") // <= I am here and 'current' still is just "u0006"
我的代码进入了if-clause,因此"u0006"被识别,但由于某种原因它没有被删除。
我在这里做错了什么?
回答
由于string是不可变的,它没有被改变。调用 时Replace,它返回一个新的string但不更新当前的。
current = current.Replace("u0006",string.Empty);
THE END
二维码