如何在 C# 中将特殊字符替换为其等效字符(例如将“a”替换为“á”)?
我需要从 Excel 文件中获取葡萄牙语文本内容,并创建一个 xml,该 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以尝试类似的方法,
这会分解文本中的重音符号,过滤它们并创建一个新字符串。组合变音符号位于 非空格标记 unicode 类别中。
You could try something like
This decomposes accents from the text, filters them and creates a new string. Combining diacritics are in the Non spacing mark unicode category.
为了供将来参考,这正是我最终得到的结果:
For future reference, this is exactly what I ended up with:
使用此解决方案性能更好:
The perform is better with this solution: