Determine a string contains unicode Character in c#

If you need a function to determine different language such as Chinese/Japanese/French and so on. You may want to use the following function to determine if the input contains Unicode character.

Unicode strings use two bytes per character (or more), ANSI strings only one byte per character. ASCII only extends up to 127. 128..255 is commonly used for ANSI characters. Above that is only Unicode. Some characters from value 128 and above (including 128 itself) map to a unicode character with a value higher than 255 in codepage 1252.

public static bool IsUnicode(string input)
{
      const int MaxAnsiCode = 255;

      return input.ToCharArray().Any(c => c > MaxAnsiCode);
}
Advertisements