开发者

Making State abbreviations from State Names

开发者 https://www.devze.com 2023-01-20 11:29 出处:网络
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

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开发者_StackOverflow中文版 following x50:

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

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


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
    }


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";
        }


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");
}


You should use a Dictionary<String, String> with StringComparer.OrdinalIgnoreCase.


Nothing wrong with offered solutions here but I would like to suggest another option which, in my view, is a bit better. You could define the following Enum extension method which uses reflection and Description attribute to get the full state name:

namespace System
{
    public static class EnumExtensions
    {
        public static string GetDescription(this Enum value)
        {
            var fi = value.GetType().GetField(value.ToString());
            var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
            return (attributes.Length > 0) ? attributes[0].Description : value.ToString();
        }
    }
}

Basically this will allow you to do the following:

var stateName = State.AL.GetDescription();

Now all you need is to decorate your enumerator values with Description attribute:

public enum State
{
   [Description("Alabama")
   AL,
   [Description("Alaska")
   AK
}

And the extension method will give you the full name of the state. If description is not defined it will give you the Enum value name for example for Alaska it would be AK.

This way you have a single piece of code that allows you to fetch descriptive values for any enumerator not just for states. All you need to do is define Description of Enum values that need to be more descriptive and the extension will do the rest.

NOTE: The extension has to be placed in the System namespace for it to work out of the box without having to reference it's namespace.


Here is a tested C# function which takes the US state abbreviation and returns the full state name. It uses a dictionary method which is more compact than a switch. To answer your question, you can just exchange the keys and values.

https://heuristically.wordpress.com/2011/04/19/csharp-dictionary-us-state-abbreivation-names-using-sas/

public string stateAbbreviationExpand(string abbr)
{
    Dictionary<string, string> states = new Dictionary<string, string>();

    states.Add("AL", "Alabama");
    states.Add("AK", "Alaska");
    states.Add("AZ", "Arizona");
    states.Add("AR", "Arkansas");
    states.Add("CA", "California");
    states.Add("CO", "Colorado");
    states.Add("CT", "Connecticut");
    states.Add("DE", "Delaware");
    states.Add("DC", "District of Columbia");
    states.Add("FL", "Florida");
    states.Add("GA", "Georgia");
    states.Add("HI", "Hawaii");
    states.Add("ID", "Idaho");
    states.Add("IL", "Illinois");
    states.Add("IN", "Indiana");
    states.Add("IA", "Iowa");
    states.Add("KS", "Kansas");
    states.Add("KY", "Kentucky");
    states.Add("LA", "Louisiana");
    states.Add("ME", "Maine");
    states.Add("MD", "Maryland");
    states.Add("MA", "Massachusetts");
    states.Add("MI", "Michigan");
    states.Add("MN", "Minnesota");
    states.Add("MS", "Mississippi");
    states.Add("MO", "Missouri");
    states.Add("MT", "Montana");
    states.Add("NE", "Nebraska");
    states.Add("NV", "Nevada");
    states.Add("NH", "New Hampshire");
    states.Add("NJ", "New Jersey");
    states.Add("NM", "New Mexico");
    states.Add("NY", "New York");
    states.Add("NC", "North Carolina");
    states.Add("ND", "North Dakota");
    states.Add("OH", "Ohio");
    states.Add("OK", "Oklahoma");
    states.Add("OR", "Oregon");
    states.Add("PA", "Pennsylvania");
    states.Add("RI", "Rhode Island");
    states.Add("SC", "South Carolina");
    states.Add("SD", "South Dakota");
    states.Add("TN", "Tennessee");
    states.Add("TX", "Texas");
    states.Add("UT", "Utah");
    states.Add("VT", "Vermont");
    states.Add("VA", "Virginia");
    states.Add("WA", "Washington");
    states.Add("WV", "West Virginia");
    states.Add("WI", "Wisconsin");
    states.Add("WY", "Wyoming");
    if (states.ContainsKey(abbr))
        return (states[abbr]);
    /* error handler is to return an empty string rather than throwing an exception */
    return "";      
}


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

so if you are going to go this route I would:

  1. use a switch statement instead of if else switch(state.ToLower()). This will be more efficient than if then statements. The compiler will optimize the switch statement.

  2. If you absolutely must use an if then statement. Do

    string lowerCaseState = state.ToLower().
    if(lowerCaseState == "new york"){....}else if...

This way you are creating a lower case string once (strings are immutable) instead of each part of the if then statement.

In truth, I would probably use a switch statement with a static method.

  1. State names aren't going to change
  2. State abbreviations aren't going to change.

You could create an object to store the values to load them each time the program runs, but why? You might as well let the compiler optimize access for non-changing static values.


This is technically off topic, but I'm picking if someone's after province 2 letter codes, then country is next. here's how you do it to get the 2 letter country codes, having first the country name:

    protected string GetCountryCode(string country)
    {
        var v1 = (from c in CultureInfo.GetCultures(CultureTypes.SpecificCultures)
                  where (new RegionInfo(c.LCID)).EnglishName != ""
                  || (new RegionInfo(c.LCID)).EnglishName != null
                  orderby (new RegionInfo(c.LCID)).EnglishName ascending
                  select new
                  {
                      Code = (new RegionInfo(c.LCID)).TwoLetterISORegionName,
                      Name = (new RegionInfo(c.LCID)).EnglishName
                  }).Distinct();

        string code = v1.Where(t => t.Name.ToLower() == country.ToLower()).FirstOrDefault().Code;

        if (!string.IsNullOrEmpty(code))
            return code;
        else return "";
    }


A database with the state name field indexed (clustered index if that's the field you're going to search on most often) so that lookup would be efficient and a query returning the state abbreviation:

select s.[StateAbbreviation]
from [dbo].[State] s
where s.[StateName] = @StateName;

This has the benefit of being reusable across many applications and environments.


If you use a .ini file, you can get the abbreviation with one line The following will return FL for MyAbrev

MyAbrev = sGetINI(App.Path & "\" & "SSApp.ini", "States", "Florida","No State")

You must have the API call for sGetINI and the following listing for all 50 states in your .ini file

[States]
Alabama=AL
Alaska=AK
Arizona=AZ
Arkansas=AR
.
.
.
Washington=WA
West Virginia=WV
Wisconsin=WI
Wyoming=WY

If you want to get the opposite, ie, States for Abbreviations, add the following to the list.

AL=Alabama
AK=Alaska
AZ=Arizona
AR=Arkansas
.
.
.
WA=Washington
WV=West Virginia
WI=Wisconsin
WY=Wyoming


Here is a class I wrote to convert different styles to ISO3166 and can also be used for this case. If looping over many records, I would use a dictionary that is initialized once or this can be adapted to do so.

using System;

namespace Core.ISO
{
    internal class UsStateCode_Iso3166
    {
        private UsStateCode_Iso3166(string name,
                                    string isoCode,
                                    string alpha2)
        {
            Name = name;
            IsoCode = isoCode;
            Alpha2 = alpha2;
        }

        public string Name { get; }
        public string IsoCode { get; }
        public string Alpha2 { get; }

        public static UsStateCode_Iso3166 Alabama { get { return new UsStateCode_Iso3166("Alabama", "US-AL", "AL"); } }
        public static UsStateCode_Iso3166 Alaska { get { return new UsStateCode_Iso3166("Alaska", "US-AK", "AK"); } }
        public static UsStateCode_Iso3166 AmericanSamoa { get { return new UsStateCode_Iso3166("American Samoa", "US-AS", "AS"); } }
        public static UsStateCode_Iso3166 Arizona { get { return new UsStateCode_Iso3166("Arizona", "US-AZ", "AZ"); } }
        public static UsStateCode_Iso3166 Arkansas { get { return new UsStateCode_Iso3166("Arkansas", "US-AR", "AR"); } }
        public static UsStateCode_Iso3166 California { get { return new UsStateCode_Iso3166("California", "US-CA", "CA"); } }
        public static UsStateCode_Iso3166 Colorado { get { return new UsStateCode_Iso3166("Colorado", "US-CO", "CO"); } }
        public static UsStateCode_Iso3166 Connecticut { get { return new UsStateCode_Iso3166("Connecticut", "US-CT", "CT"); } }
        public static UsStateCode_Iso3166 Delaware { get { return new UsStateCode_Iso3166("Delaware", "US-DE", "DE"); } }
        public static UsStateCode_Iso3166 DistrictOfColumbia { get { return new UsStateCode_Iso3166("District Of Columbia", "US-DC", "DC"); } }
        public static UsStateCode_Iso3166 Florida { get { return new UsStateCode_Iso3166("Florida", "US-FL", "FL"); } }
        public static UsStateCode_Iso3166 Georgia { get { return new UsStateCode_Iso3166("Georgia", "US-GA", "GA"); } }
        public static UsStateCode_Iso3166 Guam { get { return new UsStateCode_Iso3166("Guam", "US-GU", "GU"); } }
        public static UsStateCode_Iso3166 Hawaii { get { return new UsStateCode_Iso3166("Hawaii", "US-HI", "HI"); } }
        public static UsStateCode_Iso3166 Idaho { get { return new UsStateCode_Iso3166("Idaho", "US-ID", "ID"); } }
        public static UsStateCode_Iso3166 Illinois { get { return new UsStateCode_Iso3166("Illinois", "US-IL", "IL"); } }
        public static UsStateCode_Iso3166 Indiana { get { return new UsStateCode_Iso3166("Indiana", "US-IN", "IN"); } }
        public static UsStateCode_Iso3166 Iowa { get { return new UsStateCode_Iso3166("Iowa", "US-IA", "IA"); } }
        public static UsStateCode_Iso3166 Kansas { get { return new UsStateCode_Iso3166("Kansas", "US-KS", "KS"); } }
        public static UsStateCode_Iso3166 Kentucky { get { return new UsStateCode_Iso3166("Kentucky", "US-KY", "KY"); } }
        public static UsStateCode_Iso3166 Louisiana { get { return new UsStateCode_Iso3166("Louisiana", "US-LA", "LA"); } }
        public static UsStateCode_Iso3166 Maine { get { return new UsStateCode_Iso3166("Maine", "US-ME", "ME"); } }
        public static UsStateCode_Iso3166 Maryland { get { return new UsStateCode_Iso3166("Maryland", "US-MD", "MD"); } }
        public static UsStateCode_Iso3166 Massachusetts { get { return new UsStateCode_Iso3166("Massachusetts", "US-MA", "MA"); } }
        public static UsStateCode_Iso3166 Michigan { get { return new UsStateCode_Iso3166("Michigan", "US-MI", "MI"); } }
        public static UsStateCode_Iso3166 Minnesota { get { return new UsStateCode_Iso3166("Minnesota", "US-MN", "MN"); } }
        public static UsStateCode_Iso3166 Mississippi { get { return new UsStateCode_Iso3166("Mississippi", "US-MS", "MS"); } }
        public static UsStateCode_Iso3166 Missouri { get { return new UsStateCode_Iso3166("Missouri", "US-MO", "MO"); } }
        public static UsStateCode_Iso3166 Montana { get { return new UsStateCode_Iso3166("Montana", "US-MT", "MT"); } }
        public static UsStateCode_Iso3166 Nebraska { get { return new UsStateCode_Iso3166("Nebraska", "US-NE", "NE"); } }
        public static UsStateCode_Iso3166 Nevada { get { return new UsStateCode_Iso3166("Nevada", "US-NV", "NV"); } }
        public static UsStateCode_Iso3166 NewHampshire { get { return new UsStateCode_Iso3166("New Hampshire", "US-NH", "NH"); } }
        public static UsStateCode_Iso3166 NewJersey { get { return new UsStateCode_Iso3166("New Jersey", "US-NJ", "NJ"); } }
        public static UsStateCode_Iso3166 NewMexico { get { return new UsStateCode_Iso3166("New Mexico", "US-NM", "NM"); } }
        public static UsStateCode_Iso3166 NewYork { get { return new UsStateCode_Iso3166("New York", "US-NY", "NY"); } }
        public static UsStateCode_Iso3166 NorthCarolina { get { return new UsStateCode_Iso3166("North Carolina", "US-NC", "NC"); } }
        public static UsStateCode_Iso3166 NorthDakota { get { return new UsStateCode_Iso3166("North Dakota", "US-ND", "ND"); } }
        public static UsStateCode_Iso3166 NorthernMarianaIslands { get { return new UsStateCode_Iso3166("Northern Mariana Islands", "US-MP", "MP"); } }
        public static UsStateCode_Iso3166 Ohio { get { return new UsStateCode_Iso3166("Ohio", "US-OH", "OH"); } }
        public static UsStateCode_Iso3166 Oklahoma { get { return new UsStateCode_Iso3166("Oklahoma", "US-OK", "OK"); } }
        public static UsStateCode_Iso3166 Oregon { get { return new UsStateCode_Iso3166("Oregon", "US-OR", "OR"); } }
        public static UsStateCode_Iso3166 Pennsylvania { get { return new UsStateCode_Iso3166("Pennsylvania", "US-PA", "PA"); } }
        public static UsStateCode_Iso3166 PuertoRico { get { return new UsStateCode_Iso3166("Puerto Rico", "US-PR", "PR"); } }
        public static UsStateCode_Iso3166 RhodeIsland { get { return new UsStateCode_Iso3166("Rhode Island", "US-RI", "RI"); } }
        public static UsStateCode_Iso3166 SouthCarolina { get { return new UsStateCode_Iso3166("South Carolina", "US-SC", "SC"); } }
        public static UsStateCode_Iso3166 SouthDakota { get { return new UsStateCode_Iso3166("South Dakota", "US-SD", "SD"); } }
        public static UsStateCode_Iso3166 Tennessee { get { return new UsStateCode_Iso3166("Tennessee", "US-TN", "TN"); } }
        public static UsStateCode_Iso3166 Texas { get { return new UsStateCode_Iso3166("Texas", "US-TX", "TX"); } }
        public static UsStateCode_Iso3166 UnitedStatesMinorOutlyingIslands { get { return new UsStateCode_Iso3166("United States Minor Outlying Islands", "US-UM", "UM"); } }
        public static UsStateCode_Iso3166 Utah { get { return new UsStateCode_Iso3166("Utah", "US-UT", "UT"); } }
        public static UsStateCode_Iso3166 Vermont { get { return new UsStateCode_Iso3166("Vermont", "US-VT", "VT"); } }
        public static UsStateCode_Iso3166 VirginIslands { get { return new UsStateCode_Iso3166("Virgin Islands", "US-VI", "VI"); } }
        public static UsStateCode_Iso3166 Virginia { get { return new UsStateCode_Iso3166("Virginia", "US-VA", "VA"); } }
        public static UsStateCode_Iso3166 Washington { get { return new UsStateCode_Iso3166("Washington", "US-WA", "WA"); } }
        public static UsStateCode_Iso3166 WestVirginia { get { return new UsStateCode_Iso3166("West Virginia", "US-WV", "WV"); } }
        public static UsStateCode_Iso3166 Wisconsin { get { return new UsStateCode_Iso3166("Wisconsin", "US-WI", "WI"); } }
        public static UsStateCode_Iso3166 Wyoming { get { return new UsStateCode_Iso3166("Wyoming", "US-WY", "WY"); } }
        public static UsStateCode_Iso3166 None { get { return new UsStateCode_Iso3166("N/A", "", ""); } }

        public static UsStateCode_Iso3166 FromString(string str)
        {
            return str switch
            {
                "" => None,
                "ALABAMA" => Alabama,
                "ALASKA" => Alaska,
                "AMERICAN SAMOA" => AmericanSamoa,
                "ARIZONA" => Arizona,
                "ARKANSAS" => Arkansas,
                "CALIFORNIA" => California,
                "COLORADO" => Colorado,
                "CONNECTICUT" => Connecticut,
                "DELAWARE" => Delaware,
                "DISTRICT OF COLUMBIA" => DistrictOfColumbia,
                "FLORIDA" => Florida,
                "GEORGIA" => Georgia,
                "GUAM" => Guam,
                "HAWAII" => Hawaii,
                "IDAHO" => Idaho,
                "ILLINOIS" => Illinois,
                "INDIANA" => Indiana,
                "IOWA" => Iowa,
                "KANSAS" => Kansas,
                "KENTUCKY" => Kentucky,
                "LOUISIANA" => Louisiana,
                "MAINE" => Maine,
                "MARYLAND" => Maryland,
                "MASSACHUSETTS" => Massachusetts,
                "MICHIGAN" => Michigan,
                "MINNESOTA" => Minnesota,
                "MISSISSIPPI" => Mississippi,
                "MISSOURI" => Missouri,
                "MONTANA" => Montana,
                "NEBRASKA" => Nebraska,
                "NEVADA" => Nevada,
                "NEW HAMPSHIRE" => NewHampshire,
                "NEW JERSEY" => NewJersey,
                "NEW MEXICO" => NewMexico,
                "NEW YORK" => NewYork,
                "NORTH CAROLINA" => NorthCarolina,
                "NORTH DAKOTA" => NorthDakota,
                "NORTHERN MARIANA ISLANDS" => NorthernMarianaIslands,
                "OHIO" => Ohio,
                "OKLAHOMA" => Oklahoma,
                "OREGON" => Oregon,
                "PENNSYLVANIA" => Pennsylvania,
                "PUERTO RICO" => PuertoRico,
                "RHODE ISLAND" => RhodeIsland,
                "SOUTH CAROLINA" => SouthCarolina,
                "SOUTH DAKOTA" => SouthDakota,
                "TENNESSEE" => Tennessee,
                "TEXAS" => Texas,
                "UNITED STATES MINOR OUTLYING ISLANDS" => UnitedStatesMinorOutlyingIslands,
                "UTAH" => Utah,
                "VERMONT" => Vermont,
                "VIRGIN ISLANDS" => VirginIslands,
                "VIRGINIA" => Virginia,
                "WASHINGTON" => Washington,
                "WEST VIRGINIA" => WestVirginia,
                "WISCONSIN" => Wisconsin,
                "WYOMING" => Wyoming,
                "AL" => Alabama,
                "AK" => Alaska,
                "AS" => AmericanSamoa,
                "AZ" => Arizona,
                "AR" => Arkansas,
                "CA" => California,
                "CO" => Colorado,
                "CT" => Connecticut,
                "DE" => Delaware,
                "DC" => DistrictOfColumbia,
                "FL" => Florida,
                "GA" => Georgia,
                "GU" => Guam,
                "HI" => Hawaii,
                "ID" => Idaho,
                "IL" => Illinois,
                "IN" => Indiana,
                "IA" => Iowa,
                "KS" => Kansas,
                "KY" => Kentucky,
                "LA" => Louisiana,
                "ME" => Maine,
                "MD" => Maryland,
                "MA" => Massachusetts,
                "MI" => Michigan,
                "MN" => Minnesota,
                "MS" => Mississippi,
                "MO" => Missouri,
                "MT" => Montana,
                "NE" => Nebraska,
                "NV" => Nevada,
                "NH" => NewHampshire,
                "NJ" => NewJersey,
                "NM" => NewMexico,
                "NY" => NewYork,
                "NC" => NorthCarolina,
                "ND" => NorthDakota,
                "MP" => NorthernMarianaIslands,
                "OH" => Ohio,
                "OK" => Oklahoma,
                "OR" => Oregon,
                "PA" => Pennsylvania,
                "PR" => PuertoRico,
                "RI" => RhodeIsland,
                "SC" => SouthCarolina,
                "SD" => SouthDakota,
                "TN" => Tennessee,
                "TX" => Texas,
                "UM" => UnitedStatesMinorOutlyingIslands,
                "UT" => Utah,
                "VT" => Vermont,
                "VI" => VirginIslands,
                "VA" => Virginia,
                "WA" => Washington,
                "WV" => WestVirginia,
                "WI" => Wisconsin,
                "WY" => Wyoming,
                "US-AL" => Alabama,
                "US-AK" => Alaska,
                "US-AS" => AmericanSamoa,
                "US-AZ" => Arizona,
                "US-AR" => Arkansas,
                "US-CA" => California,
                "US-CO" => Colorado,
                "US-CT" => Connecticut,
                "US-DE" => Delaware,
                "US-DC" => DistrictOfColumbia,
                "US-FL" => Florida,
                "US-GA" => Georgia,
                "US-GU" => Guam,
                "US-HI" => Hawaii,
                "US-ID" => Idaho,
                "US-IL" => Illinois,
                "US-IN" => Indiana,
                "US-IA" => Iowa,
                "US-KS" => Kansas,
                "US-KY" => Kentucky,
                "US-LA" => Louisiana,
                "US-ME" => Maine,
                "US-MD" => Maryland,
                "US-MA" => Massachusetts,
                "US-MI" => Michigan,
                "US-MN" => Minnesota,
                "US-MS" => Mississippi,
                "US-MO" => Missouri,
                "US-MT" => Montana,
                "US-NE" => Nebraska,
                "US-NV" => Nevada,
                "US-NH" => NewHampshire,
                "US-NJ" => NewJersey,
                "US-NM" => NewMexico,
                "US-NY" => NewYork,
                "US-NC" => NorthCarolina,
                "US-ND" => NorthDakota,
                "US-MP" => NorthernMarianaIslands,
                "US-OH" => Ohio,
                "US-OK" => Oklahoma,
                "US-OR" => Oregon,
                "US-PA" => Pennsylvania,
                "US-PR" => PuertoRico,
                "US-RI" => RhodeIsland,
                "US-SC" => SouthCarolina,
                "US-SD" => SouthDakota,
                "US-TN" => Tennessee,
                "US-TX" => Texas,
                "US-UM" => UnitedStatesMinorOutlyingIslands,
                "US-UT" => Utah,
                "US-VT" => Vermont,
                "US-VI" => VirginIslands,
                "US-VA" => Virginia,
                "US-WA" => Washington,
                "US-WV" => WestVirginia,
                "US-WI" => Wisconsin,
                "US-WY" => Wyoming,
                _ => throw new ArgumentOutOfRangeException(nameof(str)),
            };
        }
    }
}

It can be called like below...

    void test()
    {
        UsStateCode_Iso3166 stateByName = UsStateCode_Iso3166.FromString("New York");
        UsStateCode_Iso3166 stateByAbbrev = UsStateCode_Iso3166.FromString("NY");
        UsStateCode_Iso3166 stateByIso = UsStateCode_Iso3166.FromString("US-NY");

        Console.WriteLine($"Name: {stateByAbbrev.Name}, Abbrev: {stateByAbbrev.Alpha2}, ISO3166: {stateByAbbrev.IsoCode}");
        //output: Name: New York, Abbrev: NY, ISO3166: US-NY
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消