如何用C#中的等效字符替换特殊字符(例如“á”代表“a”)? [英] How to replace special characters with their equivalent (such as " á " for " a") in C#?

查看:157
本文介绍了如何用C#中的等效字符替换特殊字符(例如“á”代表“a”)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从Excel文件中获取葡萄牙语文本内容,并创建一个将由不支持ç,á,é等字符的应用程序使用的xml其他。我不能只删除字符,而是用它们的等同物(例如c,a,e)替换。

I need to get the Portuguese text content out of an Excel file and create an xml which is going to be used by an application that doesn't support characters such as "ç", "á", "é", and others. And I can't just remove the characters, but replace them with their equivalent ("c", "a", "e", for example).

我假设有一个更好的方法来单独检查每个字符,并替换它们的同行。

I assume there's a better way to do it than check each character individually and replace it with their counterparts. Any suggestions on how to do it?

推荐答案

您可以尝试类似

var decomposed = "áéö".Normalise(NormalizationForm.FormD);
var filtered = decomposed.Where(c => char.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark);
var newString = new String(filtered.ToArray());

这会从文本中分解重音,过滤它们并创建一个新的字符串。组合变音符号位于非间距标记 unicode类别中。

This decomposes accents from the text, filters them and creates a new string. Combining diacritics are in the Non spacing mark unicode category.

这篇关于如何用C#中的等效字符替换特殊字符(例如“á”代表“a”)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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