C#控制台应用程序,从两个文本文件中搜索匹配的字符串[]

时间:2017-06-07 03:15:33

标签: c#

我有两个文本文件:具有

的animal.txt
number,letter,color,animal
1,a,green,alligator
2,b,brown,bear
3,c,black,cat
4,d,white,dog
5,e,pink,elephant

和habit.txt有

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

我的代码到目前为止要求输入数字和字母。并使用字符串[]和分割来搜索文本文件。

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

2 个答案:

答案 0 :(得分:0)

String.Contains("some text")

我建议您使用数据库来编写所需数据的脚本,而不是像长字符串一样站起来的.text文件。

答案 1 :(得分:0)

class Program
{
    static void Main(string[] args)
    {
        int counter = 0;
        string line;
        string number;
        string letter;
        // this assumes that there will always be only one record??
        bool lineFound;

        string animalType = null;
        string animalColor = null;

        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(',');
                // your example data shows the number in position 0 and the letter in position 1
                if ((number == words[0]) && (letter == words[1]))
                {
                    Console.WriteLine(line);

                    // assign the found animals type and color to use later

                    animalType = words[3];
                    animalColor = words[2];

                    lineFound = true;
                }

                counter++;
            }

            if (!lineFound)
            {
                Console.WriteLine("Invalid number and/or letter");
            }

            file.Close();

            System.IO.StreamReader habitatFile = new System.IO.StreamReader("habitat.txt");
            // line is being used by two different streams and can create unexpected behavior so instead create a seperate variable
            while ((line = habitatFile.ReadLine()) != null)
            {
                string[] words = line.Split(',');
                // your example data shows the number in position 0 and the letter in position 1
                if ((animalColor == words[0]) && (animalType == words[1]))
                {
                    Console.WriteLine(line);
                }
            }

            habitatFile.Close();
        }
        while (!lineFound);
    }
}

但是我会考虑将这些文本系列放到课堂上,看看你能做些什么更容易(例如,如果你可以通过颜色搜索动物呢?)

public class Animal
{
    public int Number { get; set; }

    public string Letter { get; set; }

    public string Color { get; set; }

    // CSV file has listed as 'animal'
    public string Name { get; set; }
}

public class Habitat
{
    public string Color { get; set; }

    // CSV file has listed as 'animal'
    public string Name { get; set; }

    public string Found { get; set; }

    public string Diet { get; set; }

}