字符串替换在C#变音符号 [英] String replace diacritics in C#

查看:175
本文介绍了字符串替换在C#变音符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用方法来创建用户友好的URL。因为我的网站是在克罗地亚,有我不希望剥夺,但与另一个替换他们的字符。例如前,该字符串:结果
ŠĐĆŽšđčćž结果
必须是:
sdccz-sdccz
搜索结果
所以,我想使两个数组,一个将包含将被替换和其他阵列替换字符的字符:结果

I'd like to use this method to create user-friendly URL. Because my site is in Croatian, there are characters that I wouldn't like to strip but replace them with another. Fore example, this string:
ŠĐĆŽ šđčćž
needs to be: sdccz-sdccz

So, I would like to make two arrays, one that will contain characters that are to be replaced and other array with replacement characters:

string[] character = { "Š", "Đ", "Č", "Ć", "Ž", "š", "đ", "č", "ć", "ž" };
string[] characterReplace = { "s", "d", "c", "c", "z", "s", "d", "c", "c", "z" };



最后,这两个数组应该是某种方法的使用,将采取字符串,找到匹配并替换它们。在PHP我用的preg_replace函数来处理这个问题。在C#这不工作:结果

Finally, this two arrays should be use in some method that will take string, find matches and replace them. In php I used preg_replace function to deal with this. In C# this doesn't work:

s = Regex.Replace(s, character, characterReplace);



结果
将不胜感激,如果有人能够提供帮助。
谢谢


Would appreciate if someone could help. Thanks

推荐答案

看来你要去掉符号和离开基地的性格。我推荐的本路翎的解决方案这里这样的:

It seems you want to strip off diacritics and leave the base character. I'd recommend Ben Lings's solution here for this:

string input = "ŠĐĆŽ šđčćž";
string decomposed = input.Normalize(NormalizationForm.FormD);
char[] filtered = decomposed
    .Where(c => char.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
    .ToArray();
string newString = new String(filtered);



编辑:小问题!它不为DJ工作。其结果是:

Slight problem! It doesn't work for the Đ. The result is:

SĐCZ sđccz

这篇关于字符串替换在C#变音符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆