简体   繁体   English

如何检查一个字符串变量中的每个字母?

[英]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.

相关问题 如何在.net中的正则表达式中获取2个字母7个数字1个字母格式的字符串 - how can I get 2 alphabet 7 numeric 1 alphabet format for a string in regex in .net 如何添加到列表中的每个项目(字符串)<int, string> 动态id(int)实际上是里面每个元素的位置? - How can I add to every item (string) inside of the List<int, string> dynamic id (int) which is actually position of every element inside? 我如何检查该字符串变量表示一个点结构? - how can i check that string variable represent a point struct? 如何将双引号添加到变量内的字符串? - How can I add double quotes to a string that is inside a variable? 我如何检查vector2(触摸输入)是否在rectTransform []数组的每个rectTransform内部? - How can i check if a vector2 (touch input) is inside every rectTransform of a rectTransform[] array? 如何将一个字符串数组的每个元素与另一个字符串数组的每个元素进行比较? - How can I compare each element of one string array to every element of another string array? 如何将数字转换为字母,将字母转换为字符串中的数字? - How to exchange numbers to alphabet and alphabet to numbers in a string? 如何检查文本文件中是否包含多行文本? - How can i check if a text file contain more then one line of text inside? 我如何检查SqlDataReader中的变量是什么? - How can i check what is variable into the SqlDataReader? 如何检查变量是否为字符串 - How to check if the variable is a String
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM