在文本文件中搜索特定单词并在其上显示行 [英] Searching for a Specific Word in a Text File and Displaying the line its on

查看:53
本文介绍了在文本文件中搜索特定单词并在其上显示行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试用C#在文本文件中查找单词时遇到麻烦.

I am having trouble attempting to find words in a text file in C#.

我想找到输入到控制台中的单词,然后显示在控制台中上找到的单词的整行.

I want to find the word that is input into the console then display the entire line that the word was found on in the console.

在我的文本文件中,我有:

In my text file I have:

斯蒂芬·哈伦(Stephen Haren),12月9,4055551235

劳拉·克劳辛,1月23,4054447788

威廉·康纳,12月13日,123456789

卡拉·玛丽(Kara Marie),10月23日,1593574862

奥黛丽·卡里特(Audrey Carrit),1月16日,1684527548

塞巴斯蒂安·贝克(Sebastian Baker),10月23日,9184569876

因此,如果我输入"December",我希望它显示"Stephen Haren,December,9,4055551235"和"William Connor,December,13,123456789".

So if I input "December" I want it to display "Stephen Haren,December,9,4055551235" and "William Connor,December,13,123456789" .

我考虑过要使用子字符串,但是我发现必须有一种更简单的方法.

I thought about using substrings but I figured there had to be a simpler way.

给出答案后的我的代码:

My Code After 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's 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();
    }
}

推荐答案

遍历所有行(StreamReader,File.ReadAllLines等),然后检查是否 line.Contains("December")(用用户输入替换"December").

Iterate through all the lines (StreamReader, File.ReadAllLines, etc.) and check if line.Contains("December") (replace "December" with the user input).

如果您有大文件,我将使用StreamReader.并使用@Matias Cicero的IndexOf-Example代替,因为不区分大小写.

I would go with the StreamReader in case you have large files. And use the IndexOf-Example from @Matias Cicero instead of contains for case insensitive.

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

这篇关于在文本文件中搜索特定单词并在其上显示行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆