String.Replace not replacing apostrophe
Solution 1
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("’", "'");
Solution 2
The replace doesn't work because ’
and '
are not the same character.
And maybe you forgot to capture the result, your code is too short to tell.
Solution 3
’
and '
are different characters. You also need to assign it somewhere (strings are immutable), Replace()
returns new string:
myString = myString.Replace("’", "'");
Solution 4
Since strings are immutable, you need to assign your result back to another string.
string original = "/news/2012/march/cameron’s-crackdown-on-whiplash-–-why-the-minimum-speed-requirement-is-oddly-suspicious";
string updated = original.Replace("’","'");
(note also that ` and ’ are not the same)
Solution 5
Your are replacing '
instead of ’
. Also remember that strings are immutable, so you must assign the result to a new variable in case you want to store it.
Funky
Updated on January 22, 2020Comments
-
Funky over 4 years
I'm trying to replace apostrophes with a string, for some reason the method just doesn't find the apostrophe in the string. Here is the URL that just doesn't seem to work:
"/news/2012/march/cameron’s-crackdown-on-whiplash-–-why-the-minimum-speed-requirement-is-oddly-suspicious" .Replace("'", "'");
Does anyone have any ideas?