locked
remove "ال" or "اَلْ" or "الْ" or "اَل" in from first string in arab word RRS feed

  • Question

  • User2131089582 posted

    remove "ال" or "اَلْ" or "الْ" or "اَل" in from first string in arab word

    اَلْفَرَقَ will return فَرَقَ
    اَلفَرَقَ will return فَرَقَ
    الْفَرَقَ will return فَرَقَ
    الفَرَقَ will return فَرَقَ

    here is what i have tried
    public static string RemoveAl(string input)
    {
    if (input.StartsWith("ال"))
    return input = input.Substring(2);

    if (input.StartsWith("اَلْ"))
    return input = input.Substring(4);

    if (input.StartsWith("الْ") || input.StartsWith("اَل"))
    return input = input.Substring(3);

    return input;
    }

    but i think this is not good way is there good way to do that?

    Monday, October 7, 2019 12:03 AM

All replies

  • User-17257777 posted

    Hi hocamahdi99,

    Your code runs flawlessly. However, in order to avoid hard-coding the lengths of prefix, you could change your code in following way:

    public string RemoveAl(string input)
            {
                string[] arr = { "ال", "اَلْ", "الْ", "اَل" };
                foreach(var i in arr)
                { 
                    if (input.StartsWith(i))
                    {
                       return input = input.Substring(i.Length);
                    }
                }
                return input;
            }

    Best Regards,

    Jiadong Meng

    Monday, October 7, 2019 9:42 AM