This is how I'd do it:
private static String states = "|AL|AK|AS|AZ|AR|CA|CO|CT|DE|DC|FM|FL|GA|GU|HI|ID|IL|IN|IA|KS|KY|LA|ME|MH|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|MP|OH|OK|OR|PW|PA|PR|RI|SC|SD|TN|TX|UT|VT|VI|VA|WA|WV|WI|WY|";
public static bool isStateAbbreviation (String state)
{
return state.Length == 2 && states.IndexOf( state ) > 0;
}
The method utilizes an optimized system routine, which uses a single machine instruction for doing the search. Plus, with words without fixed lengths, it is advisable to check for "|" + state + "|" so that you can ensure not hitting a substring but only a complete match. This might take some time because of the string concatenation but can still find a match without much delay. And, for validating abbreviations in lowercase as well as uppercase, you could either check state.UpperCase(), or double the string 'states' for including any lowercase variants. And, it uses the least amount of memory despite any number of runs and should seem more preferable than those Regex or Hashtable lookups every time.