在C#中将数字字符串转换为等效整数

在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!