Search for a specific word in a text file and display a string on it

I am having trouble finding words in a text file in C #.

I want to find the word that is entered into the console, and then display the entire line in which the word was found on the console.

In my text file I have:

Stephen Haren, December, 9.4055551235

Laura Clausing, January 23, 4054447788

William Connor, December 13, 123456789

Kara Marie, October 23,1593574862

Audrey Carrit, January 16, 2008.

Sebastian Baker, October, 23,9184569876

So, if I enter December, I want him to display Stephen Haren, 9.4055551235 and William Connor, December 13, 123456789.

I was thinking about using substrings, but I decided there should be an easier way.

My code after the given answer:

using System;
using System.IO;
class ReadFriendRecords
{
    public static void Main()
    {
        //the path of the file
        FileStream inFile = new FileStream(@"H:\C#\Chapter.14\FriendInfo.txt", FileMode.Open, FileAccess.Read);
        StreamReader reader = new StreamReader(inFile);
        string record;
        string input;
        Console.Write("Enter Friend Birth Month >> ");
        input = Console.ReadLine();
        try
        {
            //the program reads the record and displays it on the screen
            record = reader.ReadLine();
            while (record != null)
            {
                if (record.Contains(input))
                {
                    Console.WriteLine(record);
                }
                    record = reader.ReadLine();
            }
        }
        finally
        {
            //after the record is done being read, the progam closes
            reader.Close();
            inFile.Close();
        }
        Console.ReadLine();
    }
}
+7
4

(StreamReader, File.ReadAllLines ..) , line.Contains("December") ( "" ).

: StreamReader, . IndexOf-Example @Matias Cicero, .

Console.Write("Keyword: ");
var keyword = Console.ReadLine() ?? "";
using (var sr = new StreamReader("")) {
    while (!sr.EndOfStream) {
        var line = sr.ReadLine();
        if (String.IsNullOrEmpty(line)) continue;
        if (line.IndexOf(keyword, StringComparison.CurrentCultureIgnoreCase) >= 0) {
            Console.WriteLine(line);
        }
    }
}
+7

- :

//We read all the lines from the file
IEnumerable<string> lines = File.ReadAllLines("your_file.txt");

//We read the input from the user
Console.Write("Enter the word to search: ");
string input = Console.ReadLine().Trim();

//We identify the matches. If the input is empty, then we return no matches at all
IEnumerable<string> matches = !String.IsNullOrEmpty(input)
                              ? lines.Where(line => line.IndexOf(input, StringComparison.OrdinalIgnoreCase) >= 0)
                              : Enumerable.Empty<string>();

//If there are matches, we output them. If there are not, we show an informative message
Console.WriteLine(matches.Any()
                  ? String.Format("Matches:\n> {0}", String.Join("\n> ", matches))
                  : "There were no matches");

, LINQ String.IndexOf String.Contains, .

+2

@Rinecamo, :

string toSearch = Console.ReadLine().Trim();

In this encoding, you can read the user input and save it in a line, and then iterate over each line:

foreach (string  line in System.IO.File.ReadAllLines(FILEPATH))
{
    if(line.Contains(toSearch))
        Console.WriteLine(line);
}

Replace FILEPATHwith an absolute or relative path, for example. "\ File2Read.txt".

+2
source

You can use this algorithm to search for text in a file, use this code in

static void Main(string[] args)
    {
    }

Try this

StreamReader oReader;
if (File.Exists(@"C:\TextFile.txt")) 
{
Console.WriteLine("Enter a word to search");
string cSearforSomething = Console.ReadLine().Trim();
oReader = new StreamReader(@"C:\TextFile.txt");
string cColl = oReader.ReadToEnd();
string cCriteria = @"\b"+cSearforSomething+@"\b";
System.Text.RegularExpressions.Regex oRegex = new 
System.Text.RegularExpressions.Regex(cCriteria,RegexOptions.IgnoreCase);

int count = oRegex.Matches(cColl).Count;
Console.WriteLine(count.ToString());
}
Console.ReadLine();
0
source

Source: https://habr.com/ru/post/1612597/


All Articles