检测日文字符输入和“Romajis”(ASCII)

我希望能够检测到用户的时间:

  1. 输入日文字符(汉字或假名)
  2. 输入罗马字符(专用)

目前我正在使用这样的ASCII范围(C#语法):

string searchKeyWord = Console.ReadLine(); var romajis = from c in searchKeyWord where c >= ' ' && c <= '~' select c; if (romajis.Any()) { // Romajis } else { // Japanese input } 

有更好,更快(更强)……的方法吗?

编辑:该问题可以推广到具有非ascii字符集的任何其他语言。

维基百科非常好,右上角的unicode范围为平假名,片假名和汉字。 我们可以利用它来优化您的算法并获得其他字符集。

 private static IEnumerable GetCharsInRange(string text, int min, int max) { return text.Where(e => e >= min && e <= max); } 

用法:

 var romaji = GetCharsInRange(searchKeyword, 0x0020, 0x007E); var hiragana = GetCharsInRange(searchKeyword, 0x3040, 0x309F); var katakana = GetCharsInRange(searchKeyword, 0x30A0, 0x30FF); var kanji = GetCharsInRange(searchKeyword, 0x4E00, 0x9FBF); 

请注意,这应该和你一样快,只是更好/更好的imo 🙂

确定一般语言集

是的,您可以检测到这样的字符集,但不是真正的语言。 由于法语,德语等共享很多字符,英语和日语共享很多汉字(显然)。 你不能清楚地说明,如果没有巨大的查找图表,很多角色的单个字符来自单一语言。

还有一个事实是日语使用英语(和标点符号)相当多,你的方法会考虑任何包含罗马字或图释的字母是罗马字。