是否内置了.NET功能,可以用状态名称来创建状态缩写?
我知道函数编写起来并不困难,但我认为MS已经想到了比以下x50更有效的方法:
if statename.tolower = "new york" then
statename = "NY"
else if
任何其他提高效率的想法也值得赞赏。
答案 0 :(得分:75)
我希望这会有所帮助
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
}
答案 1 :(得分:16)
只需查看Homam的答案即可。我已经让switch语句返回带有标准外壳的状态。
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";
}
答案 2 :(得分:12)
再次添加到Homam's answer,我只需要一个文本翻译,所以我拿出了枚举:
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");
}
答案 3 :(得分:11)
您应该Dictionary<String, String>
使用StringComparer.OrdinalIgnoreCase
。
答案 4 :(得分:5)
这里提供的解决方案没有错,但我想建议另一种选择,在我看来,它更好一点。您可以定义以下Enum扩展方法,该方法使用reflection和Description属性来获取完整的州名:
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();
}
}
}
基本上,这将允许您执行以下操作:
var stateName = State.AL.GetDescription();
现在您只需要使用Description属性修饰枚举器值:
public enum State
{
[Description("Alabama")
AL,
[Description("Alaska")
AK
}
扩展方法将为您提供状态的全名。如果未定义描述,它将为您提供Enum值名称,例如对于Alaska,它将是AK。
通过这种方式,您只需一段代码即可为任何枚举器提取描述值,而不仅仅是状态。您需要做的就是定义Enum值的描述,这些值需要更具描述性,扩展将完成其余的工作。
注意:扩展必须放在System命名空间中,以便开箱即用,而不必引用它的命名空间。
答案 5 :(得分:3)
这是一个经过测试的C#函数,它接受美国州名缩写并返回完整的州名。它使用比开关更紧凑的字典方法。要回答您的问题,您只需交换密钥和值即可。
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 "";
}
答案 6 :(得分:2)
if statename.tolower == "new york" then
statename = "NY"
else if
所以,如果你要走这条路,我会:
使用switch语句而不是if else switch(state.ToLower())
。这比if语句更有效。编译器将优化switch语句。
如果你绝对必须使用if then语句。做
string lowerCaseState = state.ToLower()。
if(lowerCaseState ==“new york”){....}否则如果......
这样你创建一个小写字符串一次(字符串是不可变的)而不是if then语句的每一部分。
事实上,我可能会使用带有静态方法的switch语句。
您可以创建一个对象来存储值,以便在每次程序运行时加载它们,但为什么呢?您也可以让编译器优化对不变的静态值的访问。
答案 7 :(得分:1)
将状态名称字段编入索引的数据库(聚集索引,如果这是您最常搜索的字段),以便查找有效并且查询返回州缩写:
select s.[StateAbbreviation]
from [dbo].[State] s
where s.[StateName] = @StateName;
这样可以在许多应用程序和环境中重用。
答案 8 :(得分:1)
这在技术上是偏离主题的,但是如果有人在省2个字母代码之后选择,那么国家就是下一个。这里是你如何获得2个字母的国家代码,首先是国家名称:
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 "";
}
答案 9 :(得分:0)
如果你使用.ini文件,你可以用一行得到缩写 以下将为 MyAbrev 返回 FL
MyAbrev = sGetINI(App.Path & "\" & "SSApp.ini", "States", "Florida","No State")
您必须具有 sGetINI 的 API 调用以及 .ini 文件中所有 50 个状态的以下列表
[States]
Alabama=AL
Alaska=AK
Arizona=AZ
Arkansas=AR
.
.
.
Washington=WA
West Virginia=WV
Wisconsin=WI
Wyoming=WY
如果您想得到相反的结果,即缩写的状态,请将以下内容添加到列表中。
AL=Alabama
AK=Alaska
AZ=Arizona
AR=Arkansas
.
.
.
WA=Washington
WV=West Virginia
WI=Wisconsin
WY=Wyoming
答案 10 :(得分:0)
这是我写的一个类,用于将不同的样式转换为 ISO3166,也可以用于这种情况。如果循环遍历许多记录,我会使用一个初始化一次的字典,或者这可以适应这样做。
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)),
};
}
}
}
它可以像下面这样调用...
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
}