check if string exists in a file

后端 未结 7 1800
猫巷女王i
猫巷女王i 2021-02-09 19:36

I have the following piece of code which opens a text file and reads all the lines in the file and storing it into a string array.

Which then checks if

7条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-09 20:01

    You can try this:


    First you need to create a method that receives an array of type string, then we convert that array to a string, then we read all the text from the txt and we can use (Contains) to know if the text we send exists in our txt and we validate if that is truth or false, I hope it helps you.

            public static void TextValidation(string[] val){
                //Path of your file
                string path = "/Users/Desktop/YourFile.txt";
                //Array to string
                string b = string.Join(",",val);
                //Validate if exists
                if(File.ReadAllText(path).Contains(b)){
                    Console.WriteLine("found");
                    // Do something if the data is found
                }else{
                    Console.WriteLine("Not found");
                }
            }
    

提交回复
热议问题