简体   繁体   中英

How can i check every alphabet inside one string variable?

How can i check every alphabet inside one string variable? I am trying to make a program that can translate sentences into ones and zeros. look at the code below, that is how much i have done right now. i can read values from a text file and write out the values for only one letter. i want to know how i can write my code to be able to check every letter in a sentence.![enter image description here][1]

Dictionary<string, string> dictionary = new Dictionary<string, string>();


string[] lines = File.ReadAllLines("Values.txt");

for (int i = 0; i < 4; i++)
{
    string value = lines[i].Substring(4);
    string identifier = lines[i].Substring(0, 1);
    dictionary.Add(identifier, value);
}

string sentence = Console.ReadLine();
if (sentence == "A")
{
    Console.WriteLine(dictionary[sentence]);
}
else if (sentence == "B")
{
    Console.WriteLine(dictionary[sentence]);
}


Console.ReadKey();

If you want iterate over chars in your sentence variable you can use foreach:

foreach (var item in sentence)
{
    ....
}

In this example item is variable of char type. You can call ToString() method to convert it to string.

Since you tagged this as C# all you really have to do is read from the string like an array since C# allows indexing

String wordzyo = "ABCDEF";
char aLeter = wordzyo[0];  // read "A" into CHAR "aLeter"

If this is Java and not C# you would use "aLeter.charAt(0);"

Don't know why your asking this? It's been asked like a hundred times before...

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM