Welcome to Ultra Developers.

the company that developes your success

Read Blog


How to: Check that a string contains Arabic Characters using C#

How to: Check that a string contains Arabic Characters using C#

Sometimes your application may need to know if a string contains Arabic characters for example if you create an application that sends SMS messages, you need to know the language of the message to handle payment and the length of SMS message differ from Arabic and English messages. You can do that by using the following utility helper method:

internal static bool ContainsArabicLetters(string text)
{
    foreach (char character in text.ToCharArray())
    {
        if (character >= 0x600 && character <= 0x6ff)
            return true;

        if (character >= 0x750 && character <= 0x77f)
            return true;

        if (character >= 0xfb50 && character <= 0xfc3f)
            return true;

        if (character >= 0xfe70 && character <= 0xfefc)
            return true;
    }
    return false;
}

There is a way to check that a string contains Arabic characters with regular expressions as in the following method:

internal bool HasArabicCharacters(string text)
{
    Regex regex = new Regex(
        "[\u0600-\u06ff]|[\u0750-\u077f]|[\ufb50-\ufc3f]|[\ufe70-\ufefc]");
    return regex.IsMatch(text);
} 

This method simply checks that every character in the input text falls in the Unicode Character Code of Arabic characters. You can find all Arabic Unicode Character Codes in the following link http://www.unicode.org/charts/

Similar Posts