我正在尝试用字符串替换撇号,由于某种原因,该方法在字符串中找不到撇号。这是似乎不起作用的 URL:
"/news/2012/march/cameron’s-crackdown-on-whiplash-–-why-the-minimum-speed-requirement-is-oddly-suspicious"
.Replace("'", "'");
有没有人有任何想法?
由于字符串是不可变的,因此您需要将结果分配回另一个字符串。
string original = "/news/2012/march/cameron’s-crackdown-on-whiplash-–-why-the-minimum-speed-requirement-is-oddly-suspicious";
string updated = original.Replace("’","'");
(还要注意 ` 和 ' 是不一样的)
Strings are immutable types. You can't change them. Even if you think you change them, you create a new strings object. String.Replace()
method also returns a new string by the way.
Try to assign in a new string reference with "’"
not "'"
.
string str = "/news/2012/march/cameron’s-crackdown-on-whiplash-–-why-the-minimum-speed-requirement-is-oddly-suspicious".Replace("’", "'");
只需将结果分配给变量
var str = "...".Replace("'", "'");