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 bool 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…