I am making a quiz and have pulled a series of strings from a text file and added them a list, further separating the file info into individual strings. My question is, how would I make the individual strings correspond to a numerical value? For example A = 1, B = 2, so on and so forth.
The following code depicts the creation of the list and the adding of elements:
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);
}
}
How would this be done?
In general terms, you are looking for a dictionary. In this case, we are using it to define the mapping between strings and numbers:
Dictionary<string, int> mapping = new Dictionary<string, int>();
mapping.Add("A", 1);
...
int value = mapping["A"];
You could take advantage of the ASCII table if you just want to convert the first few capital letters to numbers:
int value = (int)stringValue[0] - (int)'A' + 1;
Assuming each "string" is a single letter, such as A
, B
, and so on, you can set up an enum
and parse each letter into it's appropriate enum value:
public enum Letter
{
A = 1,
B = 2,
C = 3,
D = 4,
E = 5,
F = 6,
G = 7,
H = 8,
I = 9,
J = 10
}
You only have to split the string once, it puts all values into an array, and you can foreach
through that and build up your list:
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);
}
To convert and use it as an int, you can just cast it:
Letter letter = Letter.A;
int a = (int)letter;
use an enum
enum Keys
{
A=1,
B,
C,
//continue onward
}
To convert to/from string:
string s = Keys.B.ToString();
Keys key = (Keys)Enum.Parse(typeof(Keys), s);
To convert to/from int:
int i = (int)Keys.B;
Keys keyFromI = (Keys)i;
You don't need to add enum
or dictionary
for the alphabet !
static void Main(string[] args)
{
string intialString = "abc".ToUpper();
string numberString = "";
foreach (char c in intialString)
{
numberString += (int)c - 64;
}
Console.WriteLine(numberString);
}
Here is clear example. If you want use it !
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.