As a programmer, sometimes the thing you need most is something painfully simple. It may be a thing you’ve done a hundred times, but if you don’t have a code locker where you keep these snippets of code, it can be maddening trying to track down what program you wrote that bit of code in. That is why we are starting the code library section of Vandelay Web to house some of these important pieces of reusable code to keep us organized and to help out any programmers who just need a quick hit.
Today, we start with something just about every programmer has had to do at one point in time: convert a U.S. state abbreviation to the full state name. Its nothing more than a glorified state lookup table, but one that gets used over and over and over again. Below is the switch statement tripping all those cases written in c#. We’ve housed it inside a method for cleanliness. The switch statement sends back the original abbreviation if for some reason it didn’t resolve (U.S. territory perhaps?). Quick, dirty and on your way.
public static string ConvertState(string state){ switch (state){ case "AL": return "Alabama"; case "AK": return "Alaska"; case "AR": return "Arkansas"; case "AZ": return "Arizona"; case "CA": return "California"; case "CO": return "Colorado"; case "CT": return "Connecticut"; case "DE": return "Delaware"; case "FL": return "Florida"; case "GA": return "Georgia"; case "HI": return "Hawaii"; case "ID": return "Idaho"; case "IL": return "Illinois"; case "IN": return "Indiana"; case "IA": return "Iowa"; case "KS": return "Kansas"; case "KY": return "Kentucky"; case "LA": return "Louisiana"; case "ME": return "Maine"; case "MD": return "Maryland"; case "MA": return "Massachusetts"; case "MI": return "Michigan"; case "MN": return "Minnesota"; case "MS": return "Mississippi"; case "MO": return "Missouri"; case "MT": return "Montana"; case "NE": return "Nebraska"; case "NV": return "Nevada"; case "NH": return "New Hampshire"; case "NJ": return "New Jersey"; case "NM": return "New Mexico"; case "NY": return "New York"; case "NC": return "North Carolina"; case "ND": return "North Dakota"; case "OH": return "Ohio"; case "OK": return "Oklahoma"; case "OR": return "Oregon"; case "PA": return "Pennsylvania"; case "RI": return "Rhode Island"; case "SC": return "South Carolina"; case "SD": return "South Dakota"; case "TN": return "Tennessee"; case "TX": return "Texas"; case "UT": return "Utah"; case "VT": return "Vermont"; case "VA": return "Virginia"; case "WA": return "Washington"; case "WV": return "West Virginia"; case "WI": return "Wisconsin"; case "WY": return "Wyoming"; default: return state; } }
4 Comments on "Code Library: c# Switch Statement for State Names & Abbreviations"
Joe Aucoin
October 14, 2014Great snippet . . . .
Typo on "Nebrask" though . . .
Joe Aucoin
October 14, 2014Oh, I added Canada for you . . .
// CANADA
case "AB":
return "Alberta";
case "BC":
return "British Columbia";
case "MB":
return "Manitoba";
case "NB":
return "New Brunswick";
case "NL":
return "Newfoundland and Labrador";
case "NS":
return "Nova Scotia";
case "NT":
return "Northwest Territories";
case "NU":
return "Nunavut";
case "ON":
return "Ontario";
case "PE":
return "Prince Edward Island";
case "QC":
return "Quebec";
case "SK":
return "Saskatchewan";
case "YT":
return "Yukon";
ketaki
December 18, 2014Thanks a lot man... saved me a great amount of time...
Dan
September 13, 2016Thank you!!