开发者

C# String.Replace and UTF-8 chars

I am trying to replace some of the Romanian characters in this string with my own chars, but it's not really working.

static void Main(string[] args)
{
    string sometext = "Încă nu s-a pornit acasă";
    some开发者_运维技巧text.Replace("ă", "1");

    Console.WriteLine(sometext);
    Console.ReadKey(true);
}

This outputs the original sometext without any changes. However, neither without replacing nor with replacing the final result is Inca nu s-a pornit acasa. Diacritics are replaced with the ISO-8859-1 characters corresponding to them. Î becomes I, ă becomes a.

Sumarry:

The expected result is: Înc1 nu s-a pornit acas1.

Actually, I get: Inca nu s-a pornit acasa

Note: In Advanced Save Options, I've selected the following encoding: Unicode (UTF-8 with signature) - Codepage 65001

Acutally, strings are immutable and String.Replace returns a string, so sometext = sometext.Replace("ă", "1"); works just fine. Thanks to all!


Replace does not mutate the string, it returns a string with the replacements so your code should be:

sometext = sometext.Replace(...);


static void Main(string[] args)
{
    string sometext = "Încă nu s-a pornit acasă";
    sometext = sometext.Replace("ă", "1");

    Console.WriteLine(sometext);
    Console.ReadKey(true);
}

or

static void Main(string[] args)
{
    string sometext = "Încă nu s-a pornit acasă".Replace("ă", "1");

    Console.WriteLine(sometext);
    Console.ReadKey(true);
}

or whatever, Replace returns the string


Strings are immutable. So do sometext = sometext.Replace(...).


static void Main(string[] args)
{
    string sometext = "Încă nu s-a pornit acasă";
    sometext = sometext.Replace("ă", "1");

    Console.WriteLine(sometext);
    Console.ReadKey(true);
}

try this...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜