Fastest way possible to validate a string to be only Alphas, or a given string set
I need the absoulute fastest way possible to validate an input string against a given rule. In this case lest say Alpha only characters.
I can think of a number of ways both verbose and non verbose. However speed in execution is of the essence. So If anybody can offer their pearls of wisdom I would be massivly grateful.
I'm avoiding regex to get away from the overhead of creating the expression object. However am open to revisit this if people think this is the FASTEST option.
Current Ideas include: 1)
internal static b开发者_JS百科ool Rule_AlphaOnly(string Value)
{
char[] charList = Value.ToCharArray();
for (int i = 0; i < charList.Length; i++)
{
if (!((charList[i] >= 65 && charList[i] <= 90) || (charList[i] >= 97 && charList[i] <= 122)))
{
return false;
}
}
return true;
}
2)
char[] charList = Value.ToCharArray();
return charList.All(t => ((t >= 65 && t <= 90) || (t >= 97 && t <= 122)));
Thought about also using the "Contains" methods.
Any ideas welcomed. Many thanks
3) for (int i = 0; i < Value.Length; i++) { if(!char.IsLetter(Value, i)) { return false; } }
Not sure on the speed of this one but what about...
foreach(char c in Value)
{
if(!char.IsLetter(c))
return false;
}
Both codes can be made more efficient by removing the ToCharArray
call and thus avoiding a copy: you can access the individual characters of a string directly.
Of those two ways I would strongly opt for the second unless you can show that it’s too slow. Only then would I switch to the first, but replace the for
loop with a foreach
loop.
Oh, and neither of your codes treats Unicode correctly. If you had used a regular expression with a proper character class, then this wouldn’t be an issue. Correctness first, performance second.
After C# compiler and JIT are done optimizing it, I doubt this will be much slower than manual for
loop:
return Value.All(char.IsLetter);
If you need to check for arbitrary set of characters, do something like this:
var set = new HashSet<char>(new[] { 'a', 'b', 'c' /* Etc... */ });
return Value.All(set.Contains);
Unless the set
is trivial and can be "emulated" efficiently via few if
s, the hashtable lookup is bound to be as fast a solution as it gets.
精彩评论