6

Basically what I'm trying to do is replace a symbol like ™, ® etc with something else but when I call

myString = myString.Replace("®", "something else")

Its doesn't do anything

Any Ideas?

3
  • 1
    Are you remembering to re-assign the string? Commented Aug 24, 2011 at 16:10
  • 1
    Yes I'm later reassigning the string. I'll edit the question to make that more clear. Commented Aug 24, 2011 at 16:17
  • @Killie01 thanks for some reason I didn't think the symbol was encoded and it was. The following ended up working for me name.Replace("\u00AE", "<sup>\u00AE</sup>") Commented Aug 24, 2011 at 16:40

7 Answers 7

8

try myString.Replace("\u00A9", "else"); you have to escape the ©

Sign up to request clarification or add additional context in comments.

2 Comments

Hey man you were right! I needed the encoded version of the symbol. Thanks a bunch!
oh 00A9 = the copyright mark, i thought it was what you wanted, 00AE is indeed the r thingy
6

When you use String.Replace you create a new string. It is a very common mistake to believe the the supplied string is modified. However, strings in .NET are immutable and cannot be modified.

You have to call it like this:

myString = myString.Replace("®", "something else");

Comments

2

I assume that your mistake is in calling Replace without assigning the result to anything.

without seeing your code is difficult to guess, but something like this should work:

myString = myString.Replace("®", "something else");

Comments

1

It may be likely that C# does not like the literal registered symbol. I would suggest trying to replace the character by using a character code, using either the integral value, hex, or unicode.

Below is an example using the integral value of the character ®.

string originalString = "whatever®";
string stuff = "something else";
char registered = (char)174;
string replacedString = originalString.Replace(registered, stuff);

ref: http://msdn.microsoft.com/en-us/library/x9h8tsay.aspx

Comments

0

Remember that string.Replace returns a new string, so you need to re-assign it

myString = myString.Replace("®", "something else");

Comments

0

Try to use Unicode characters to replace this symbols.

        string x = "® ™ dwdd2";
        string y = x.Replace('\u00AE', 'X');

It's working ;-)

http://msdn.microsoft.com/en-us/library/aa664669%28v=vs.71%29.aspx

And a list of Charakters:

http://en.wikipedia.org/wiki/List_of_Unicode_characters

2 Comments

Thats trash invalidsyntax... I'm replacing ® with the char X... maybe you should read msdn msdn.microsoft.com/en-us/library/czx8s9ts.aspx
I ended up using 'code'name = name.Replace("\u00AE", "<sup>\u00AE</sup>");'code'
0

It works for me:

var myString = "Hello world ®, will this work?";
var result = myString.Replace("®", "something else");
Console.WriteLine(result);

results in:

Hello world something else, will this work?

You can see it run here.

Does your original string really contain that character or does it contain something like an html entity: &reg; or &#174; or other "encoded" version of that character?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.