Validating String against USPS state abbreviations

0 votes

I have googled this up without any luck so far, what I have to do is validate a string against the list of possible United States Postal Service state abbreviations. And, the obvious solution would be to write a code with a really lengthy if (or switch) statement that checks all the 50 states. But, as I said, it's body would have to be very lengthy. So, what is an easier way of doing the same, like maybe by using a RegEx or an enumerator object?

I'm using C# and .net 3.5 and here's the List of USPS State Abbreviations

Nov 9, 2018 in Others by Bharani
• 4,550 points
73 views

1 answer to this question.

0 votes

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.

answered Nov 9, 2018 by DataKing99
• 8,130 points

Related Questions In Others

0 votes
1 answer

Parsing a string with GetOpt::Long::GetOptions

Hey, have a look at the section ...READ MORE

answered Nov 15, 2018 in Others by nirvana
• 3,060 points
123 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Is there a .NET equivalent to Apache Hadoop?

Microsoft dropped its alternative (Dryad) in favor of Hadoop. ...READ MORE

answered Sep 18, 2018 in Big Data Hadoop by Frankie
• 9,810 points
47 views
0 votes
1 answer

Validate String against USPS State Abbreviations

Try something like this: private static String states ...READ MORE

answered Sep 20, 2018 in IoT (Internet of Things) by Annie97
• 2,190 points
24 views
0 votes
1 answer

Authenticate on an ASP.Net Forms Authorization website from a console app

Essentially, we need to record a regular ...READ MORE

answered Sep 20, 2018 in IoT (Internet of Things) by Annie97
• 2,190 points
36 views
0 votes
2 answers

Transforming a key/value string into distinct rows in R

We would start off by loading the ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,550 points
61 views
0 votes
1 answer
+1 vote
2 answers

Should I commit Terraform State files to the git repository?

Its better not to commit it to ...READ MORE

answered Aug 3, 2018 in DevOps & Agile by Nilesh
• 6,880 points
475 views