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

查看:173
本文介绍了如何用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天全站免登陆