Here comes another great string extension for improved readability. You all know how to check if a string starts with another string (yep, it's the StartsWith method I'm talking about).
But what if you wan't to check if a string starts with any of a series of strings? Well, you'd have to do something like this.
string name = "Mr. Markus Olsson" var l = new List<string> { "Dr", "Mr", "Ms" }; bool found; foreach(string s in l) { if(name.StartsWith(l)) { found = true; break; } }
Or, you could use lambdas for a much more elegant solution
string name = "Mr. Markus Olsson" var l = new List<string> { "Dr", "Mr", "Ms" }; bool found = l.Exists(prefix => name.StartsWith(prefix));
That's pretty cool, right? The .Exists method on the list object takes a Predicate
Enter StartsWithAnyOf extension methods
/// <summary> /// Checks to see if the string starts with any of the supplied strings /// </summary> /// <param name="s">The string to check for a start value</param> /// <param name="strings">One or more strings</param> /// <returns>True the strings starts with any of the supplied strings, false otherwise</returns> public static bool StartsWithAnyOf(this string s, params string[] strings) { if (s == null) throw new ArgumentNullException("s"); if (strings == null) throw new ArgumentNullException("strings"); if (strings.Length == 0) throw new ArgumentOutOfRangeException("strings", "You must supply one or more strings"); return Array.Exists(strings, (prefix => s.StartsWith(prefix)); } /// <summary> /// Checks to see if the string starts with any of the supplied strings /// </summary> /// <param name="s">The string to check for a start value</param> /// <param name="strings">One or more strings</param> /// <returns>True the strings starts with any of the supplied strings, false otherwise</returns> public static bool StartsWithAnyOf(this string s, List<string> strings) { if (s == null) throw new ArgumentNullException("s"); if (strings == null) throw new ArgumentNullException("strings"); if (strings.Count == 0) throw new ArgumentOutOfRangeException("strings", "You must supply one or more strings"); return strings.Exists(x => s.StartsWith(x)); }
This allows us to rewrite our code to
string name = "Mr. Markus Olsson" bool found = name.StartsWithAnyOf("Dr.", "Mr.", "Ms.");
Readability in a nutshell. Variations of these extension methods includes an override that takes a StringComparison in order to allow for case insensitive lookup. The EndsWithAnyOf method is of course also a must.