开发者

C# String.Replace not finding / replacing Symbol (™, ®)

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?


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


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");


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");


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


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

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


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


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: ® or ® or other "encoded" version of that character?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜