Jul*_*ien 4 c# unicode ascii char non-ascii-characters
我希望能够检测到用户:
目前,我正在使用像这样的ASCII范围(C#语法):
string searchKeyWord = Console.ReadLine();
var romajis = from c in searchKeyWord where c >= ' ' && c <= '~' select c;
if (romajis.Any())
{
// Romajis
}
else
{
// Japanese input
}
Run Code Online (Sandbox Code Playgroud)
有没有更好,更快(更强...)的方法来做到这一点?
编辑:问题可以推广到具有非ASCII字符集的任何其他语言。
Wikipedia非常好,并且在右上角具有用于平假名,片假名和汉字的unicode范围。我们可以利用这个优势来优化您的算法,并获得其他字符集。
private static IEnumerable<char> GetCharsInRange(string text, int min, int max)
{
return text.Where(e => e >= min && e <= max);
}
Run Code Online (Sandbox Code Playgroud)
用法:
var romaji = GetCharsInRange(searchKeyword, 0x0020, 0x007E);
var hiragana = GetCharsInRange(searchKeyword, 0x3040, 0x309F);
var katakana = GetCharsInRange(searchKeyword, 0x30A0, 0x30FF);
var kanji = GetCharsInRange(searchKeyword, 0x4E00, 0x9FBF);
Run Code Online (Sandbox Code Playgroud)
请注意,这应该和您的速度一样快,只是更好一点/更好的imo :)
是的,您可以检测到类似的字符集,但不能检测语言。由于法语,德语等与英语共享许多字符,而日语与中文(显然)共享许多汉字。您不能清楚地说一个字符来自一种语言,而对于许多字符却没有庞大的查找表。
还有一个事实,就是日语大量使用英语(和标点符号),您的方法会将包含罗马字或表情符号的任何内容视为罗马字。
| 归档时间: |
|
| 查看次数: |
4231 次 |
| 最近记录: |