簡體   English   中英

C#控制台應用程序,從兩個文本文件中搜索匹配的string []

[英]C# Console Application, search for matching string[] from two text files

我有兩個文本文件: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

到目前為止,我的代碼要求輸入數字和字母。 並使用string []搜索文本文件並拆分。

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
String.Contains("some text")

我建議您使用數據庫來編寫所需數據的腳本,而不要使用像長字符串一樣站立的.text文件。

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; }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM