我有兩個文本文件匹配字符串[]:有C#控制檯應用程序,搜索來自兩個文本文件
number,letter,color,animal
1,a,green,alligator
2,b,brown,bear
3,c,black,cat
4,d,white,dog
5,e,pink,elephant
有
colour,animal,found,diet
green,alligator,swamp,fish
green,alligator,swamp,bird
brown,bear,forest,fruit
black,cat,home,catfood
white,dog,home,dogfood
pink,elephant,space,spacefruit
我的代碼animal.txt和habit.txt到目前爲止要求一個號碼和一封信。並使用字符串[]和分割搜索文本文件。
class Program
{
static void Main(string[] args)
{
int counter = 0;
string line;
string number;
string letter;
bool lineFound = false;
do
{
Console.WriteLine("Enter number");
number = Console.ReadLine();
Console.WriteLine("\nEnter letter");
letter = Console.ReadLine();
System.IO.StreamReader file = new System.IO.StreamReader("animal.txt");
while ((line = file.ReadLine()) != null)
{
string[] words = line.Split(',');
if ((number == words[1]) && (letter == words[0]))
{
Console.WriteLine(line);
lineFound = true;
}
counter++;
}
if (!lineFound)
{
Console.WriteLine("Invalid number and/or letter");
}
file.Close();
}
while (!lineFound);
取決於輸入它會顯示顏色和動物的行。 如何製作它,以便它將搜索另一個文件,habit.txt以查找animal.txt中的匹配行。例如輸入可以是「1」和「a」,控制檯將顯示
green,alligator,swamp,fish
green,alligator,swamp,bird