![](/img/trans.png)
[英]how can I get 2 alphabet 7 numeric 1 alphabet format for a string in regex in .net
[英]How can i check every alphabet inside one string variable?
如何檢查一個字符串變量中的每個字母? 我正在嘗試制作一個可以將句子轉換為一和零的程序。 看下面的代碼,這就是我現在所做的。 我可以從文本文件中讀取值,並只寫出一個字母的值。 我想知道如何編寫代碼以檢查句子中的每個字母。![在此處輸入圖片描述] [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();
如果要遍歷句子變量中的char,可以使用foreach:
foreach (var item in sentence)
{
....
}
在此示例中,項目是char類型的變量。 您可以調用ToString()方法將其轉換為字符串。
由於您將其標記為C#,因此您真正要做的就是像數組一樣從字符串中讀取內容,因為C#允許建立索引
String wordzyo = "ABCDEF";
char aLeter = wordzyo[0]; // read "A" into CHAR "aLeter"
如果這是Java而不是C#,則應使用“ aLeter.charAt(0);”。
不知道你為什么要問這個? 有人問過一百遍...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.