4

我正在尝试用字符串替换撇号,由于某种原因,该方法在字符串中找不到撇号。这是似乎不起作用的 URL:

"/news/2012/march/cameron’s-crackdown-on-whiplash-–-why-the-minimum-speed-requirement-is-oddly-suspicious"
.Replace("'", "'");

有没有人有任何想法?

4

6 回答 6

9
于 2013-01-21T16:08:34.987 回答
5
于 2013-01-21T16:08:50.887 回答
3

由于字符串是不可变的,因此您需要将结果分配回另一个字符串。

string original = "/news/2012/march/cameron’s-crackdown-on-whiplash-–-why-the-minimum-speed-requirement-is-oddly-suspicious";
string updated = original.Replace("’","'");

(还要注意 ` 和 ' 是不一样的)

于 2013-01-21T16:08:00.420 回答
2

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("’", "'");
于 2013-01-21T16:08:54.350 回答
1
于 2013-01-21T16:10:14.997 回答
0

只需将结果分配给变量

var str = "...".Replace("'", "'");
于 2013-01-21T16:08:22.533 回答