Making State abbreviations from State Names
Just tacking onto Homam's answer. I've got the switch statement returning states with standard casing.
switch (stateCode)
{
case "AL":
return "Alabama";
case "AK":
return "Alaska";
case "AS":
return "American Samoa";
case "AZ":
return "Arizona";
case "AR":
return "Arkansas";
case "CA":
return "California";
case "CO":
return "Colorado";
case "CT":
return "Connecticut";
case "DE":
return "Delaware";
case "DC":
return "District Of Columbia";
case "FM":
return "Federated States Of Micronesia";
case "FL":
return "Florida";
case "GA":
return "Georgia";
case "GU":
return "Guam";
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 "MH":
return "Marshall Islands";
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 "MP":
return "Northern Mariana Islands";
case "OH":
return "Ohio";
case "OK":
return "Oklahoma";
case "OR":
return "Oregon";
case "PW":
return "Palau";
case "PA":
return "Pennsylvania";
case "PR":
return "Puerto Rico";
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 "VI":
return "Virgin Islands";
case "VA":
return "Virginia";
case "WA":
return "Washington";
case "WV":
return "West Virginia";
case "WI":
return "Wisconsin";
case "WY":
return "Wyoming";
}
I hope this helps
public string GetState(State state)
{
switch (state)
{
case State.AL:
return "ALABAMA";
case State.AK:
return "ALASKA";
case State.AS:
return "AMERICAN SAMOA";
case State.AZ:
return "ARIZONA";
case State.AR:
return "ARKANSAS";
case State.CA:
return "CALIFORNIA";
case State.CO:
return "COLORADO";
case State.CT:
return "CONNECTICUT";
case State.DE:
return "DELAWARE";
case State.DC:
return "DISTRICT OF COLUMBIA";
case State.FM:
return "FEDERATED STATES OF MICRONESIA";
case State.FL:
return "FLORIDA";
case State.GA:
return "GEORGIA";
case State.GU:
return "GUAM";
case State.HI:
return "HAWAII";
case State.ID:
return "IDAHO";
case State.IL:
return "ILLINOIS";
case State.IN:
return "INDIANA";
case State.IA:
return "IOWA";
case State.KS:
return "KANSAS";
case State.KY:
return "KENTUCKY";
case State.LA:
return "LOUISIANA";
case State.ME:
return "MAINE";
case State.MH:
return "MARSHALL ISLANDS";
case State.MD:
return "MARYLAND";
case State.MA:
return "MASSACHUSETTS";
case State.MI:
return "MICHIGAN";
case State.MN:
return "MINNESOTA";
case State.MS:
return "MISSISSIPPI";
case State.MO:
return "MISSOURI";
case State.MT:
return "MONTANA";
case State.NE:
return "NEBRASKA";
case State.NV:
return "NEVADA";
case State.NH:
return "NEW HAMPSHIRE";
case State.NJ:
return "NEW JERSEY";
case State.NM:
return "NEW MEXICO";
case State.NY:
return "NEW YORK";
case State.NC:
return "NORTH CAROLINA";
case State.ND:
return "NORTH DAKOTA";
case State.MP:
return "NORTHERN MARIANA ISLANDS";
case State.OH:
return "OHIO";
case State.OK:
return "OKLAHOMA";
case State.OR:
return "OREGON";
case State.PW:
return "PALAU";
case State.PA:
return "PENNSYLVANIA";
case State.PR:
return "PUERTO RICO";
case State.RI:
return "RHODE ISLAND";
case State.SC:
return "SOUTH CAROLINA";
case State.SD:
return "SOUTH DAKOTA";
case State.TN:
return "TENNESSEE";
case State.TX:
return "TEXAS";
case State.UT:
return "UTAH";
case State.VT:
return "VERMONT";
case State.VI:
return "VIRGIN ISLANDS";
case State.VA:
return "VIRGINIA";
case State.WA:
return "WASHINGTON";
case State.WV:
return "WEST VIRGINIA";
case State.WI:
return "WISCONSIN";
case State.WY:
return "WYOMING";
}
throw new Exception("Not Available");
}
}
public State GetStateByName(string name)
{
switch (name.ToUpper())
{
case "ALABAMA":
return State.AL;
case "ALASKA":
return State.AK;
case "AMERICAN SAMOA":
return State.AS;
case "ARIZONA":
return State.AZ;
case "ARKANSAS":
return State.AR;
case "CALIFORNIA":
return State.CA;
case "COLORADO":
return State.CO;
case "CONNECTICUT":
return State.CT;
case "DELAWARE":
return State.DE;
case "DISTRICT OF COLUMBIA":
return State.DC;
case "FEDERATED STATES OF MICRONESIA":
return State.FM;
case "FLORIDA":
return State.FL;
case "GEORGIA":
return State.GA;
case "GUAM":
return State.GU;
case "HAWAII":
return State.HI;
case "IDAHO":
return State.ID;
case "ILLINOIS":
return State.IL;
case "INDIANA":
return State.IN;
case "IOWA":
return State.IA;
case "KANSAS":
return State.KS;
case "KENTUCKY":
return State.KY;
case "LOUISIANA":
return State.LA;
case "MAINE":
return State.ME;
case "MARSHALL ISLANDS":
return State.MH;
case "MARYLAND":
return State.MD;
case "MASSACHUSETTS":
return State.MA;
case "MICHIGAN":
return State.MI;
case "MINNESOTA":
return State.MN;
case "MISSISSIPPI":
return State.MS;
case "MISSOURI":
return State.MO;
case "MONTANA":
return State.MT;
case "NEBRASKA":
return State.NE;
case "NEVADA":
return State.NV;
case "NEW HAMPSHIRE":
return State.NH;
case "NEW JERSEY":
return State.NJ;
case "NEW MEXICO":
return State.NM;
case "NEW YORK":
return State.NY;
case "NORTH CAROLINA":
return State.NC;
case "NORTH DAKOTA":
return State.ND;
case "NORTHERN MARIANA ISLANDS":
return State.MP;
case "OHIO":
return State.OH;
case "OKLAHOMA":
return State.OK;
case "OREGON":
return State.OR;
case "PALAU":
return State.PW;
case "PENNSYLVANIA":
return State.PA;
case "PUERTO RICO":
return State.PR;
case "RHODE ISLAND":
return State.RI;
case "SOUTH CAROLINA":
return State.SC;
case "SOUTH DAKOTA":
return State.SD;
case "TENNESSEE":
return State.TN;
case "TEXAS":
return State.TX;
case "UTAH":
return State.UT;
case "VERMONT":
return State.VT;
case "VIRGIN ISLANDS":
return State.VI;
case "VIRGINIA":
return State.VA;
case "WASHINGTON":
return State.WA;
case "WEST VIRGINIA":
return State.WV;
case "WISCONSIN":
return State.WI;
case "WYOMING":
return State.WY;
}
throw new Exception("Not Available");
}
public enum State
{
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
}
You should use a Dictionary<String, String>
with StringComparer.OrdinalIgnoreCase
.
Again adding onto Homam's answer, I just needed a text translation so I took out the enums:
public string GetStateByName(string name)
{
switch (name.ToUpper())
{
case "ALABAMA":
return "AL";
case "ALASKA":
return "AK";
case "AMERICAN SAMOA":
return "AS";
case "ARIZONA":
return "AZ";
case "ARKANSAS":
return "AR";
case "CALIFORNIA":
return "CA";
case "COLORADO":
return "CO";
case "CONNECTICUT":
return "CT";
case "DELAWARE":
return "DE";
case "DISTRICT OF COLUMBIA":
return "DC";
case "FEDERATED STATES OF MICRONESIA":
return "FM";
case "FLORIDA":
return "FL";
case "GEORGIA":
return "GA";
case "GUAM":
return "GU";
case "HAWAII":
return "HI";
case "IDAHO":
return "ID";
case "ILLINOIS":
return "IL";
case "INDIANA":
return "IN";
case "IOWA":
return "IA";
case "KANSAS":
return "KS";
case "KENTUCKY":
return "KY";
case "LOUISIANA":
return "LA";
case "MAINE":
return "ME";
case "MARSHALL ISLANDS":
return "MH";
case "MARYLAND":
return "MD";
case "MASSACHUSETTS":
return "MA";
case "MICHIGAN":
return "MI";
case "MINNESOTA":
return "MN";
case "MISSISSIPPI":
return "MS";
case "MISSOURI":
return "MO";
case "MONTANA":
return "MT";
case "NEBRASKA":
return "NE";
case "NEVADA":
return "NV";
case "NEW HAMPSHIRE":
return "NH";
case "NEW JERSEY":
return "NJ";
case "NEW MEXICO":
return "NM";
case "NEW YORK":
return "NY";
case "NORTH CAROLINA":
return "NC";
case "NORTH DAKOTA":
return "ND";
case "NORTHERN MARIANA ISLANDS":
return "MP";
case "OHIO":
return "OH";
case "OKLAHOMA":
return "OK";
case "OREGON":
return "OR";
case "PALAU":
return "PW";
case "PENNSYLVANIA":
return "PA";
case "PUERTO RICO":
return "PR";
case "RHODE ISLAND":
return "RI";
case "SOUTH CAROLINA":
return "SC";
case "SOUTH DAKOTA":
return "SD";
case "TENNESSEE":
return "TN";
case "TEXAS":
return "TX";
case "UTAH":
return "UT";
case "VERMONT":
return "VT";
case "VIRGIN ISLANDS":
return "VI";
case "VIRGINIA":
return "VA";
case "WASHINGTON":
return "WA";
case "WEST VIRGINIA":
return "WV";
case "WISCONSIN":
return "WI";
case "WYOMING":
return "WY";
}
throw new Exception("Not Available");
}