How can I check if the numbers contains arabic numbers in a list of string

How can I check if the numbers contains arabic numbers in a list of string

Problem Description:

I have a list of strings , which may contain english numbers and arabic numbers.

What I need to do is to convert arabic numbers in to english numbers.

for which I am using the below code .

private string toEnglishNumber(string input)
    {
        string EnglishNumbers = "";
        for (int i = 0; i < input.Length; i++)
        {
            if (Char.IsDigit(input[i]))
            {
                EnglishNumbers += char.GetNumericValue(input, i);
            }
            else
            {
                EnglishNumbers += input[i].ToString();
            }
        }
        return EnglishNumbers;
    }

Now I need to make sure before passing my string in to this function that string contains the arabic numbers for example :

List of strings :

[
    "٠٥٥ ٥٣٠ ٨٦٨٤" , "+966557864894"
]

So instead of passing both string , I just need to pass only 0 index string which contains arabic numbers , how can I do it.

Solution – 1

You can use this method:

public static bool HasArabicGlyphs(string text)
{
    foreach (char glyph in text)
    {
        if (glyph >= 0x600 && glyph <= 0x6ff) return true;
        if (glyph >= 0x750 && glyph <= 0x77f) return true;
        if (glyph >= 0xfb50 && glyph <= 0xfc3f) return true;
        if (glyph >= 0xfe70 && glyph <= 0xfefc) return true;
    }
    return false;
}

List<string> arabicStrings = stringList.FindAll(HasArabicGlyphs);

Credits to Hans here

Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject