我怎样才能“压平” .NET 中包含宏和变音符号的文本?
我需要使搜索表单对包含宏、元音变音等的文本不敏感。
例如, “ŌōṒṓṐṑşş” 应被视为等于“oooooooo”。
在 TSQL 中,我可以让它部分工作:
select Cast('ŌōṒṓṐṑȪȫ' as varchar)
返回 Oo??????
。它足够聪明,可以将前两个字符翻译为“O”和“o”。
我试图使用此 C# 代码来“展平”文本,但它根本不起作用。结果是“??????”。
var text = "ŌōṒṓṐṑȪȫ";
var buffer = Encoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(text));
var result = Encoding.ASCII.GetString(buffer);
有没有办法在.NET 中做到这一点?我知道我可以创建一个映射,将“ŌōṒṓṐṑşş”等字符链接到“o”等其他字符,但我希望已经有一种内置方法可以做到这一点。
Possible Duplicate:
How to convert a Unicode character to its ASCII equivalent
How do I remove diacritics (accents) from a string in .NET?
I need to make a search form insensitive to text that contains macrons, umlauts, etc.
For example, "ŌōṒṓṐṑȪȫ" should be considered equal to "oooooooo".
In TSQL I'm able to get it partially working with:
select Cast('ŌōṒṓṐṑȪȫ' as varchar)
which returns Oo??????
. It is smart enough to translate the first two characters to "O" and "o".
I was trying to use this C# code to "flatten" the text but it doesn't work at all. The result is "????????".
var text = "ŌōṒṓṐṑȪȫ";
var buffer = Encoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(text));
var result = Encoding.ASCII.GetString(buffer);
Is there a way do this in .NET? I know I could create a map that links characters such as "ŌōṒṓṐṑȪȫ" to "o" and so on for other characters, but I'm hoping there is already a built-in way to do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不需要做标准化,这很耗时,而且有更好的东西。
大多数字符串比较操作都有一个需要 CompareOptions 的风格。
您可以将其用于 CompareOptions:
请参阅 CompareInfo 类 http://msdn.microsoft。 com/en-us/library/2z428sw8.aspx
You don't need to do normalization, it is time consuming, and there is something better.
Most string comparison operations have a flavor that takes a CompareOptions.
You can use this for CompareOptions:
See the CompareInfo class http://msdn.microsoft.com/en-us/library/2z428sw8.aspx
String 类有一组重载的 Normalize() 方法。
The String class has a set of overloaded Normalize() methods.