5

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?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Funky
  • 12,890
  • 35
  • 106
  • 161
  • 1
    are you definate you are assigning the value back to the string? .Replace on its own doesnt save the value back to the original string –  Jan 21 '13 at 16:07
  • Check the font in your question's `code`. You can see it's a different character, there are apostrophes, single quotes, (back)ticks... – Honza Brestan Jan 21 '13 at 16:08
  • 4
    The problem is that `'` does not equal `’`. – vgru Jan 21 '13 at 16:08
  • 1
    You don't show what you are doing with the string. Are you expecting the string to change, or do you realise `String.Replace` returns a new string? – Daniel Kelley Jan 21 '13 at 16:08
  • possible duplicate of [C# string replace does not work](http://stackoverflow.com/questions/13277667/c-sharp-string-replace-does-not-work) – Sergey Kalinichenko Jan 21 '13 at 16:09
  • You are probably looking for the [`WebUtility`](http://msdn.microsoft.com/en-us/library/ee388360.aspx) class (`HtmlEncode`/`HtmlDecode` methods), or [`HttpUtility`](http://msdn.microsoft.com/en-us/library/90d18ktz(v=vs.85).aspx) if you are targeting older .NET framewords. There is no reason to HTML-encode all these chars manually. – vgru Jan 21 '13 at 16:14

6 Answers6

9

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.

H H
  • 263,252
  • 30
  • 330
  • 514
5

and ' are different characters. You also need to assign it somewhere (strings are immutable), Replace() returns new string:

myString = myString.Replace("’", "'");
Zbigniew
  • 27,184
  • 6
  • 59
  • 66
  • Am I missing something or is one of these characters not on my keyboard. I tried using replace to enter somoe text into a sql database and the character it was failing on was ˈ and not ' . Is there a different wa of handling this that can catch all of them without copying and pasting this ˈ character into code or is that the only way? I'd never come across this before but the ˈ was after a schwa. – user609926 Nov 16 '17 at 16:14
  • @user609926 I don't understand (how you're trying to replace sql). Post a question with source code. – Zbigniew Nov 17 '17 at 16:51
3

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)

ZombieSheep
  • 29,603
  • 12
  • 67
  • 114
3

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("’", "'");
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
1

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.

Lasse Christiansen
  • 10,205
  • 7
  • 50
  • 79
0

Just assign the result to a variable

var str = "...".Replace("'", "'");
I4V
  • 34,891
  • 6
  • 67
  • 79