[英]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]
我想知道如何编写代码以检查句子中的每个字母。![在此处输入图片描述] [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: 如果要遍历句子变量中的char,可以使用foreach:
foreach (var item in sentence)
{
....
}
In this example item is variable of char type. 在此示例中,项目是char类型的变量。 You can call ToString() method to convert it to string.
您可以调用ToString()方法将其转换为字符串。
Since you tagged this as C# all you really have to do is read from the string like an array since C# allows indexing 由于您将其标记为C#,因此您真正要做的就是像数组一样从字符串中读取内容,因为C#允许建立索引
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);" 如果这是Java而不是C#,则应使用“ aLeter.charAt(0);”。
Don't know why your asking this? 不知道你为什么要问这个? It's been asked like a hundred times before...
有人问过一百遍...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.