While the question check if input is type of string has been closed two of the answers spiked a micro-optimization question in my mind: which of the below two solutions would perform better?
Reed Copsey provided a solution using Char.IsLetter
:
string myString = "RandomStringOfLetters";
bool allLetters = myString.All( c => Char.IsLetter(c) );
Adapted solution using regex from Mark Byers:
string s = "RandomStringOfLetters";
bool allLetters = Regex.IsMatch(s, "^[a-z]+$", RegexOptions.IgnoreCase);
Not wanting to just ask the question of either Reed or Mark I thought I'd write a quick test to determine which performed better. Problem is I haven't done a lot of code optimization (I tend to put code readability above all else).
Other than taking a timestamp before and after the run of each, what are some other (better?) options of determining which solution runs faster?
Edit
I modified Martin's answer to work with Console.WriteLine(...)
and ran it as a console application. Not sure exactly how LinqPad runs applications but the results were about the same:
41 178