在C#中将数字字符串转换为等效整数 [英] Convert Numeric string to equivalent integer in C#

查看:78
本文介绍了在C#中将数字字符串转换为等效整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





string [] strArray = new string [] {ONE,TWO,THREE,FOUR,FIVE, SIX,SEVEN,EIGHT,NINE,TEN};



在运行时我想将此字符串转换为等效整数。

在这里我只提到了10个字符

但运行时我的输入将大于十



i想要这样的输出

ONE - 1

TWO - 2

三 - 3

四 - 4

五 - 5



有没有办法转换成c#...?

Hi

string[] strArray = new string[] {"ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE","TEN" };

at run time i want to convert this string to equivalent integer.
here i mentioned Ten charactors only
but run time my input will be greater than "Ten"

i want output like this
"ONE" - 1
"TWO" - 2
"THREE" - 3
"FOUR" - 4
"FIVE" - 5

is there any way to convert in c#...?

推荐答案

如果您的数据是预定义的,您应该使用 enum ,如下面的代码所示,否则我看不到任何解决方案:

If your data are predefined, you should use enum like in the code bellow, otherwise I see no solution posible:
public enum NumericStrings : int
{
ZERO = 0,
ONE = 1,
TWO = 2,
ELEVEN = 11,
...
}
//The usage
string myVar = "ELEVEN"
//the conversion will be simple:
NumericStrings temp = (NumericStrings) Enum.Parse(typeof(NumericStrings), myVar);
int myInt = (int)temp; // Here is the result!


这篇关于在C#中将数字字符串转换为等效整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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