Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to check if text is in cyrillics or latin using C#?

Tags:

c#

Is there a way to check if text is in cyrillics or latin using C#?

like image 604
vikifor Avatar asked Jul 10 '12 13:07

vikifor


2 Answers

Use a Regex and check for \p{IsCyrillic}, for example:

if (Regex.IsMatch(stringToCheck, @"\p{IsCyrillic}"))
{
    // there is at least one cyrillic character in the string
}

This would be true for the string "abcабв" because it contains at least one cyrillic character. If you want it to be false if there are non cyrillic characters in the string, use:

if (!Regex.IsMatch(stringToCheck, @"\P{IsCyrillic}"))
{
    // there are only cyrillic characters in the string
}

This would be false for the string "abcабв", but true for "абв".

To check what the IsCyrillic named block or other named blocks contain, have a look at this http://msdn.microsoft.com/en-us/library/20bw873z.aspx#SupportedNamedBlocks

like image 108
Hinek Avatar answered Nov 20 '22 22:11

Hinek


How about this ?

string pattern = @"\p{IsCyrillic}";
if ( Regex.Matches(textInput, pattern).Count > 0)
{
    // contains cyrillics' characters.
}

If you want to check that contains cyrillics characters more than x characters Change the right hand numeric value.

Our system recieved spam email that contains cyrillics' characters roughly 30% of
full of text;so, couldn't decide whether 100% or 0%

like image 44
Takeo Nishioka Avatar answered Nov 20 '22 21:11

Takeo Nishioka