Check if string contains any chinese or russian characters

The two really helpful extension methods for this task:

private static readonly Regex regexChinese = new Regex(@"\p{IsCJKUnifiedIdeographs}");
public static bool IsChinese(this char s)
{
     return regexChinese.IsMatch(s.ToString());
}

private static readonly Regex regexCyrlics = new Regex(@"\p{IsCyrillic}");
public static bool IsCyrylic(this char s)
{
     return regexCyrlics.IsMatch(s.ToString());
}

We put the code to a static class and then simply write:

if (sometext.Any(z=>z.IsChinese() || z.IsCyrillic()))
     DoSomething();

You can do the same thing for some other alphabets. Check MSDN.

Advertisements

0 Responses to “Check if string contains any chinese or russian characters”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s





%d bloggers like this: