从状态名称缩写状态 [英] Making State abbreviations from State Names

查看:254
本文介绍了从状态名称缩写状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有建在.NET功能制作状态的缩写外州的名字?



我知道该功能并不难写,但我会假设该MS已经想到比下面X50更有效的方式:

 如果statename.tolower =纽约,那么
Statename的=NY
,否则,如果

使这个更高效的任何其他的想法也很受青睐。


解决方案

 如果statename.tolower ==纽约,那么
Statename的=NY
,否则,如果

所以如果你打算走这条路我会




  1. 使用的切换语句而不是其他,如果开关(state.ToLower())。这将是比如果then语句更有效。编译器将优化switch语句。


  2. 如果你绝对必须,如果再语句中使用的。做



    字符串lowerCaseState = state.ToLower()。结果
    如果(lowerCaseState ==纽约){....}否则,如果...




这样,你正在创建一个小写的字符串一次(string是不可改变的),而不是各部分如果该声明接着



在真相,我可能会使用switch语句用静态方法。




  1. 国家名称不会改变

  2. 国家缩写不会改变。



您可以创建一个对象来存储值在每次运行程序,但为什么时间来加载呢?你还不如让非改变静态值编译器优化的访问。


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.

解决方案

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.

这篇关于从状态名称缩写状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆