Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making State abbreviations from State Names

Tags:

Is there built in .NET functionality for making state abbreviations out of state names?

I know the function wouldn't be difficult to write, but I would assume that MS has thought of a more efficient way than the following x50:

if statename.tolower = "new york" then 
  statename = "NY"
else if

any other thoughts of making this more efficient are also appreciated.

like image 982
Jim Avatar asked Oct 13 '10 15:10

Jim


2 Answers

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
    }
like image 67
Homam Avatar answered Oct 18 '22 06:10

Homam


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";
        }
like image 20
erosebe Avatar answered Oct 18 '22 04:10

erosebe