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 10, 2018 in Others by Bharani
• 4,660 points
1,260 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 10, 2018 by DataKing99
• 8,240 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,130 points
686 views
0 votes
1 answer
0 votes
1 answer

Easiest way to convert int to string in C++

The C++11 version has introduced the std::stoi ...READ MORE

answered Feb 10 in Others by Rahul
• 9,000 points
38 views
0 votes
1 answer

PHP Convert String to SEO Friendly Url For Bengali Language Type

$string = preg_replace("/[^a-z0-9]/u", "$separator", $string);  change the ...READ MORE

answered Feb 14 in Others by narikkadan
• 11,280 points
117 views
0 votes
1 answer

Open S3 object as a string with Boto3

s3 = boto3.resource('s3') import boto3 s3 = obj obj(bucket, ...READ MORE

answered Mar 21 in Others by gaurav
• 17,500 points
41 views
0 votes
1 answer

How to get substring from string containing newlines

If you want to maintain the newline ...READ MORE

answered Jun 14 in C# by jyoti
• 1,240 points
18 views
0 votes
1 answer
0 votes
2 answers

Is there a .NET equivalent to Apache Hadoop?

Hadoop is a Java-based platform. So, to ...READ MORE

answered Jul 16, 2020 in Big Data Hadoop by Suhana
• 340 points
666 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,160 points
229 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP