I wouldn't say "significantly" slower, since either of them is fast enough to be effectively instantaneous to human perception.
Personally, as long as I'm not using a wildly inappropriate algorithm (like an O(N^3) linked list removal or something) my focus is always on readable, maintainable code. If there are actually performance concerns, then I'll rewrite if necessary, but in a lot of cases, the actual processing takes tiny fractions of a second and then idles waiting for user input. Whether it takes 100 nanoseconds or 10,000 nanoseconds, it's still effectively instantaneous to human perception, which finds anything less than about 100 microseconds to be "immediate".
The printing to the console is going to take orders of magnitude longer than either the regular expression or the character-based pattern matching.
You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.
Significantly slower was a measurement of performance in context with my comparison between simple string manipulation and regex itself, which has nothing to do with human perception. Also, it's not clarified as to whether he needs to validate a bunch of ID's or just a single one, so performance may or may not be accumulative here. Still, regex is not the proper choice IMHO; it wouldn't be mine.
It's a string of 6 characters in length. You wouldn't turn the barbecue back on to heat up leftover chicken the day after, you would probably just use the microwave. Same principle applies here.
You could parse the last 4 chars as an int and see if it was successful, and index[0] and [1] to check them for letters. Many ways to get the job done and still be faster but just as readable as regex.Code:public static bool ValidID(string id) { return id.Length == 6 && char.IsLetter(id[0]) && char.IsLetter(id[1]) && id.Skip(2).All(char.IsDigit); }
Last edited by cstryx; 08-29-2015 at 09:54 PM.