C# 在文本文件中搜索特定单词并在其上显示该行
我在用C#查找文本文件中的单词时遇到困难 我想找到输入到控制台中的单词,然后显示在控制台中找到该单词的整行 在我的文本文件中,我有: 斯蒂芬·哈伦,12月,94055551235 劳拉·克劳辛,一月,234054447788 威廉·康纳,12月13123456789日 卡拉玛丽,10月,231593574862 奥黛丽·卡里特,一月,161684527548 塞巴斯蒂安·贝克,十月,239184569876 因此,如果我输入“十二月”,我希望它显示“斯蒂芬·哈伦,十二月,94055551235”和“威廉·康纳,十二月,13123456789” 我考虑过使用子字符串,但我认为必须有一种更简单的方法 给出答案后的代码:C# 在文本文件中搜索特定单词并在其上显示该行,c#,text-files,C#,Text Files,我在用C#查找文本文件中的单词时遇到困难 我想找到输入到控制台中的单词,然后显示在控制台中找到该单词的整行 在我的文本文件中,我有: 斯蒂芬·哈伦,12月,94055551235 劳拉·克劳辛,一月,234054447788 威廉·康纳,12月13123456789日 卡拉玛丽,10月,231593574862 奥黛丽·卡里特,一月,161684527548 塞巴斯蒂安·贝克,十月,239184569876 因此,如果我输入“十二月”,我希望它显示“斯蒂芬·哈伦,十二月,94055551235”
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等),并检查
行。包含(“十二月”)
(用用户输入替换“十二月”)
编辑:
如果你有大文件,我会选择StreamReader。并使用@Matias Cicero中的IndexOf示例,而不是contains来表示不区分大小写。
像这样的怎么样:
//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");
//我们读取了文件中的所有行
IEnumerable lines=File.ReadAllLines(“your_File.txt”);
//我们从用户那里读取输入
Console.Write(“输入要搜索的单词:”);
字符串输入=Console.ReadLine().Trim();
//我们识别匹配项。如果输入为空,那么我们根本不返回匹配项
IEnumerable匹配=!String.IsNullOrEmpty(输入)
? 其中(line=>line.IndexOf(input,StringComparison.OrdinalIgnoreCase)>=0)
:Enumerable.Empty();
//如果存在匹配项,则输出它们。如果没有,我们将显示一条信息性消息
Console.WriteLine(匹配.Any()
?String.Format(“匹配项:\n>{0}”,String.Join(“\n>”,匹配项))
:“没有匹配”);
这种方法简单易读,它使用和,因此我们可以进行不区分大小写的搜索。如@Rinecamo所述,请尝试以下代码:
string toSearch = Console.ReadLine().Trim();
在此代码行中,您将能够读取用户输入并将其存储在一行中,然后对每行进行迭代:
foreach (string line in System.IO.File.ReadAllLines(FILEPATH))
{
if(line.Contains(toSearch))
Console.WriteLine(line);
}
将
FILEPATH
替换为绝对或相对路径,例如“\file2Read.txt”。要在文件中查找文本,可以使用此算法在中使用此代码
static void Main(string[] args)
{
}
试试这个
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();
那你呢?你是怎么读文本文件的?
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();