C# 在哪里搜索?
我目前正在做一个小的C#练习,包括一个文本文件。所有文本文件都包含文本文件中每一新行的句子。到目前为止,我能够读取文本并将其存储到字符串数组中。接下来我需要做的是搜索一个特定的词,然后写出任何包含搜索到的词/短语的句子。我只是想知道我是应该在while循环中还是在其他地方做C# 在哪里搜索?,c#,search,syntax,text-files,C#,Search,Syntax,Text Files,我目前正在做一个小的C#练习,包括一个文本文件。所有文本文件都包含文本文件中每一新行的句子。到目前为止,我能够读取文本并将其存储到字符串数组中。接下来我需要做的是搜索一个特定的词,然后写出任何包含搜索到的词/短语的句子。我只是想知道我是应该在while循环中还是在其他地方做 String filename = @"sentences.txt"; // File.OpenText allows us to read the contents of a file by establishing
String filename = @"sentences.txt";
// File.OpenText allows us to read the contents of a file by establishing
// a connection to a file stream associated with the file.
StreamReader reader = File.OpenText(filename);
if (reader == null)
{
// If we got here, we were unable to open the file.
Console.WriteLine("reader is null");
return;
}
// We can now read data from the file using ReadLine.
Console.WriteLine();
String line = reader.ReadLine();
while (line != null)
{
Console.Write("\n{0}", line);
// We can use String.Split to separate a line of data into fields.
String[] lineArray = line.Split(' ');
String sentenceStarter = lineArray[0];
line = reader.ReadLine();
}
Console.Write("\n\nEnter a term to search and display all sentences containing it: ");
string searchTerm = Console.ReadLine();
String searchingLine = reader.ReadLine();
while (searchingLine != null)
{
String[] lineArray = line.Split(' ');
String name = lineArray[0];
line = reader.ReadLine();
for (int i = 0; i < lineArray.Length; i++)
{
if (searchTerm == lineArray[0] || searchTerm == lineArray[i])
{
Console.Write("\n{0}", searchingLine.Contains(searchTerm));
}
}
}
stringfilename=@“句子.txt”;
//OpenText允许我们通过建立
//与文件关联的文件流的连接。
StreamReader=File.OpenText(文件名);
如果(读卡器==null)
{
//如果我们到了这里,就无法打开文件。
Console.WriteLine(“读卡器为空”);
返回;
}
//我们现在可以使用ReadLine从文件中读取数据。
Console.WriteLine();
字符串行=reader.ReadLine();
while(行!=null)
{
写入(“\n{0}”,第行);
//我们可以使用String.Split将一行数据分隔为字段。
String[]lineArray=line.Split(“”);
String sentenceStarter=lineArray[0];
line=reader.ReadLine();
}
Console.Write(“\n\n输入一个术语以搜索并显示包含它的所有句子:”);
字符串searchTerm=Console.ReadLine();
String searchingLine=reader.ReadLine();
while(searchingLine!=null)
{
String[]lineArray=line.Split(“”);
字符串名称=lineArray[0];
line=reader.ReadLine();
对于(int i=0;i
您可以使用文件
类来简化操作
要读取文本文件中的所有行,可以使用file.ReadAllLines
string[] lines = File.ReadAllLines("myTextFile.txt");
如果要查找包含单词或句子的所有行,可以使用Linq
// get array of lines that contain certain text.
string[] results = lines.Where(line => line.Contains("text I am looking for")).ToArray();
问题:我只是想知道我是应该在while循环中还是在其他地方执行此操作?
回答:如果您不想(也不应该)将所有文件内容存储在内存中-在while循环中。否则,您可以将while循环中的每一行复制到
列表
或数组
中,然后在其他地方搜索它们(同样,对于大文件,这是非常节省资源的方法,不推荐使用)
个人说明:您的代码看起来很奇怪(尤其是第二个
循环时-它将永远不会执行,因为文件已被读取,如果要再次读取文件,您需要重置读取器
)。首先,而
循环除了写入控制台之外,没有做任何有用的事情
如果这是真正的代码,你应该考虑修改它,并用LINQ
File.ReadLines()
避免将整个文件保存在内存中:string[]results=File.ReadLines(filename)。其中(line=>line.Contains(“我要找的文本”))。ToArray()
没有。我已经对LINQ做了任何事情。我想知道的是现在我可以在哪里搜索文本文件的所有内容。谢谢。你的建议解决了我的问题。我已经修复了我的while循环…不客气。别忘了对你的问题进行投票和/或标记为回答有用的评论(到目前为止,您有4个问题,答案很有价值,但均未被接受)本文将为您提供更多信息