搜索特定的字符串并返回整条生产线 [英] Search specific string and return whole line

查看:123
本文介绍了搜索特定的字符串并返回整条生产线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这样做的是找到一个文本文件中的字符串的所有实例,然后添加含有该字符串数组中的实线。

What I would like to do is find all instances of a string in a text file, then add the full lines containing the said string to an array.

例如:

eng    GB    English
lir    LR    Liberian Creole English
mao    NZ    Maori

搜索ENG,例如,必须在第一个两行添加到列表中,当然也包括ENG的文件中更多的实例。

Searching eng, for example, must add the first two lines to the list, including of course the many more instances of 'eng' in the file.

如何才能做到这一点,使用文本文件输入和C#?

How can this be done, using a text file input and C#?

感谢。

推荐答案

您可以使用的TextReader将读取每一行,并进行搜索,如果你发现什么ü想要的,然后添加一行到字符串数组

you can use TextReader to read each line and search for it, if you find what u want, then add that line into string array

List<string> found = new List<string>();
string line;
using(StreamReader file =  new StreamReader("c:\\test.txt"))
{
   while((line = file.ReadLine()) != null)
   {
      if(line.Contains("eng"))
      {
         found.Add(line);
      }
   }
}

也可以使用收益率回报返回enumurable

or you can use yield return to return enumurable

这篇关于搜索特定的字符串并返回整条生产线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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