2017-06-07 51 views
0

我有兩個文本文件匹配字符串[]:有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 

回答

0
String.Contains("some text") 

我建議使用一個數據庫,爲您的腳本所需的數據,而不是像長站起來.text的文件字符串。

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

} 
相關問題