.Net 中有没有办法获取 int 单词的字符串值?
例如:
(1).SomeFunction().Equals("one")
(2).SomeFunction().Equals("two")
在我正在使用的情况下,我真的只需要数字 1-9,我应该只使用开关/选择情况吗?
更新在这种情况下我也不需要本地化。
更新 2 这是我最终使用的内容:
Private Enum EnglishDigit As Integer
zero
one
two
three
four
five
six
seven
eight
nine
End Enum
(CType(someIntThatIsLessThanTen, EnglishDigit)).ToString()
For example:
(1).SomeFunction().Equals("one")
(2).SomeFunction().Equals("two")
I really only need it for digits 1-9 in the case I'm working with, should I just use a switch/select case?
Update I won't need localization in this case either.
Update 2 Here's what I ended up using:
Private Enum EnglishDigit As Integer
zero
one
two
three
four
five
six
seven
eight
nine
End Enum
(CType(someIntThatIsLessThanTen, EnglishDigit)).ToString()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
枚举怎么样?
然后您可以输入诸如...
如果您需要本地化,那么您可以添加
DescriptionAttribute
到每个枚举值。该属性的Description
属性将存储资源项的键的名称。以下函数将从属性中获取
Description
属性的值这可以通过以下方式调用:
然后您可以从资源文件中提取相关值,或者只需调用
.ToString()
如果返回null
。编辑
各种评论者指出(我必须同意),仅使用枚举值名称来引用本地化字符串会更简单。
How about an enumeration?
Then you can type things like...
If you need localization then you can add a
DescriptionAttribute
to each enum value. The attribute'sDescription
property would store the name of the resourse item's key.The following function will grab the value of the
Description
property from the attributeThis can be called in the following manner:
From that you can then pull the relevant value from the resource file, or just call
.ToString()
ifnull
was returned.Edit
Various commenters have pointed out (and I have to agree) that it would be simpler to just use the enum value names to reference localised strings.
创建字符串字典:
create a dictionary of strings:
使用查找表;一个数组就可以了。它并不比枚举慢,而且更容易本地化。
编辑
Andrey 的代码示例就是我所建议的,尽管我认为称其为字典有点令人困惑。
Use a lookup table; an array will do. It's no slower than an enum, and it's easier to localize.
edit
Andrey's code sample is what I was suggesting, although I think calling it a dictionary is a bit confusing.
如果您不需要本地化,我建议使用 Richard Ev 的解决方案。不过,对于本地化,我建议将十位数字名称添加到资源文件中,例如
NumberName_0
到NumberName_9
。这样,当查找号码时,您只需加载名称为String.Format("NumberName_{0}", mydigit)
的资源即可。顺便说一句,同样的技术也适用于可本地化的枚举名称或描述。
If you don't need localization, I'd suggest Richard Ev's solution. For localization, however, I'd suggest adding the ten digit names to a resource file, for example
NumberName_0
toNumberName_9
. This way, when looking up a number, you can just load the resource with the nameString.Format("NumberName_{0}", mydigit)
.The same technique, by the way, also works fine for localizable enumeration names or descriptions.
为什么停在 1-9...
C# 版本的 Squeak Smalltalk 对所有数字进行处理的方式:
Why stop at 1-9...
C#'s version of the way Squeak Smalltalk does it for all numbers to a vigintillion:
我认为没有任何内置函数可以做到这一点。我会使用选择案例。
I don't think there are any built-in functions to do this. I would use select case.
如果您使用的是 2008:
If you are using 2008: