将字符串列表转换为相应的int c#

时间:2014-11-04 22:15:30

标签: c# string int set match

我正在进行测验并从文本文件中提取了一系列字符串并添加了一个列表,进一步将文件信息分成单个字符串。我的问题是,如何使各个字符串对应一个数值?例如A = 1,B = 2,依此类推。

以下代码描述了列表的创建和元素的添加:

List<string> keyPool = new List<string>();
OpenFileDialog keyLoad = new OpenFileDialog();
        keyLoad.Multiselect = false;

        if (keyLoad.ShowDialog() == DialogResult.OK)
        {
            foreach (String fileName in keyLoad.FileNames)
            {

                key = File.ReadAllText(fileName);

                kLabel.Text = ("Key:" + System.Environment.NewLine);
                k1 = key.Split(new string[] { System.Environment.NewLine }, System.StringSplitOptions.RemoveEmptyEntries)[0];
                keyPool.Add(k1);
                k2 = key.Split(new string[] { System.Environment.NewLine }, System.StringSplitOptions.RemoveEmptyEntries)[1];
                keyPool.Add(k2);
                k3 = key.Split(new string[] { System.Environment.NewLine }, System.StringSplitOptions.RemoveEmptyEntries)[2];
                keyPool.Add(k3);
                k4 = key.Split(new string[] { System.Environment.NewLine }, System.StringSplitOptions.RemoveEmptyEntries)[3];
                keyPool.Add(k4);
                k5 = key.Split(new string[] { System.Environment.NewLine }, System.StringSplitOptions.RemoveEmptyEntries)[4];
                keyPool.Add(k5);
                k6 = key.Split(new string[] { System.Environment.NewLine }, System.StringSplitOptions.RemoveEmptyEntries)[5];
                keyPool.Add(k6);
                k7 = key.Split(new string[] { System.Environment.NewLine }, System.StringSplitOptions.RemoveEmptyEntries)[6];
                keyPool.Add(k7);
                k8 = key.Split(new string[] { System.Environment.NewLine }, System.StringSplitOptions.RemoveEmptyEntries)[7];
                keyPool.Add(k8);
                k9 = key.Split(new string[] { System.Environment.NewLine }, System.StringSplitOptions.RemoveEmptyEntries)[8];
                keyPool.Add(k9);
                k10 = key.Split(new string[] { System.Environment.NewLine }, System.StringSplitOptions.RemoveEmptyEntries)[9];
                keyPool.Add(k10);
            }
        }

如何做到这一点?

4 个答案:

答案 0 :(得分:3)

一般而言,您正在寻找字典。在这种情况下,我们使用它来定义字符串和数字之间的映射:

Dictionary<string, int> mapping = new Dictionary<string, int>();
mapping.Add("A", 1);
...

int value = mapping["A"];

如果您只想将前几个大写字母转换为数字,可以利用ASCII表:

int value = (int)stringValue[0] - (int)'A' + 1;

答案 1 :(得分:1)

假设每个&#34;字符串&#34;是单个字母,例如AB等,您可以设置enum并将每个字母解析为适当的枚举值:

public enum Letter
{
    A = 1,
    B = 2,
    C = 3,
    D = 4,
    E = 5,
    F = 6,
    G = 7,
    H = 8,
    I = 9,
    J = 10
}

您只需将字符串拆分一次,将所有值放入数组中,然后您可以foreach通过该字符串并构建列表:

List<Letter> keyPool = new List<Letter>();
var letters = key.Split(new string[] { System.Environment.NewLine },
    System.StringSplitOptions.RemoveEmptyEntries);

foreach(var letter in letters)
{
    keyPool.Add((Letter)Enum.Parse(typeof(Letter), letter);
}

要转换并将其用作int,您只需将其转换为:

Letter letter = Letter.A;
int a = (int)letter;

答案 2 :(得分:0)

使用enum

enum Keys
{
A=1,
B,
C,
 //continue onward
}

转换为/来自字符串:

string s = Keys.B.ToString();
Keys key = (Keys)Enum.Parse(typeof(Keys), s);

转换为/来自int:

int i = (int)Keys.B;
Keys keyFromI = (Keys)i;

答案 3 :(得分:0)

您无需为字母添加enumdictionary

    static void Main(string[] args)
    {
        string intialString = "abc".ToUpper();
        string numberString = "";

        foreach (char c in intialString)
        {
            numberString += (int)c - 64;
        }

        Console.WriteLine(numberString);
    }

这是一个明显的例子。如果你想使用它!