Fastest way possible to validate a string to be only Alphas, or a given string set?

Not sure on the speed of this one but what about foreach(char c in Value) { if(!char. IsLetter(c)) return false; }.

Not sure on the speed of this one but what about... foreach(char c in Value) { if(!char. IsLetter(c)) return false; }.

1 for correctness. – Konrad Rudolph Oct 13 at 9:24 Well this uses the for each which I believe is still slower than the for I chari looper, however will look at the char. IsLetter bit.

Thanks – DubMan Oct 13 at 11:34 @DubMan Don’t believe, measure. There is no technical reason why this should be slower than a for loop with an index. – Konrad Rudolph Oct 13 at 17:39.

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.

Actually, . NET's regex don't handle Unicode correctly. They don't work properly on anything outside the BMP (it's all just surrogates).

An example: Regex. IsMatch("𝝀", @"\p{Lu}") – Porges Oct 13 at 9:27 @Porges Good catch. But if even regular expressions don’t handle this correctly, handling this by hand is probably even harder – case in point, char.

IsLetter also handles this incorrectly. – Konrad Rudolph Oct 13 at 9:32 Yep, handling Unicode in . NET sucks.To iterate over Unicode strings you need to either use the char.

IsSurrogate methods manually, or StringInfo (which additionally will handle combining characters). You can then use char. IsLetter(string,index) on the character which will handle it correctly.

– Porges Oct 13 at 9:44 Ok then... I've gone with the following, any objections or further comments? For (int I = 0; I Length; i++) { if(!char. IsLetter(Value, i)) { return false; } } – DubMan Oct 13 at 13:24 @DubMan Hm.

Unlike @Porges I found that char. IsLetter(value, i) works no better than the other variant, at least on the example he posted. Perhaps it makes a difference for combining diacritic marks, I haven’t tested that.

– Konrad Rudolph Oct 13 at 13:32.

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. Thought about also using the "Contains" methods. Any ideas welcomed.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions