dimanche 15 avril 2012

Check if any string starts with any string defined in a string array

Once again, a simple problem

I want to check whether a string starts with any string in a list (it easy to make it with a character ).
My current implementation in C# is as follows, One of the easiest implementation is to create an extension method


Code:
public static class StringExtensions
    {
        public static bool StartsWithAny(this string str,params string[] values)
        {
            if (!string.IsNullOrEmpty(str) || values.Length > 0)
            {
                foreach (string value in values)
                {
                    if (str.StartsWith(value))
                    {
                        return true;
                    }
                }
            }
            return false;
        }
    }

Nothing really hard to understand I think Good work ;-)

Aucun commentaire:

Enregistrer un commentaire