文本文件逐行插入字符串数组



我需要帮助,试图将一个大约1000行的大型文本文档逐行放入字符串数组中。

示例:

string[] s = {firstLineHere, Secondline, etc};

我还想要一种方法来找到第一个单词,只找到这行的第一个单词。一旦找到第一个词,就复制整行。只查找第一个单词或每一行!

您可以使用File来实现这一点。ReadAllLines结合了一个小Linq(以完成Praveen回答评论中所述问题的添加。

string[] identifiers = { /*Your identifiers for needed lines*/ };
string[] allLines = File.ReadAllLines("C:test.txt");
string[] neededLines = allLines.Where(c => identifiers.Contains(c.SubString(0, c.IndexOf(' ') - 1))).ToArray();

或者让它更像一条直线:

string[] lines = File.ReadAllLines("your path").Where(c => identifiers.Contains(c.SubString(0, c.IndexOf(' ') - 1))).ToArray();

这将为您提供文档中所有行的数组,这些行以您在标识符字符串数组中定义的关键字开头。

有一种内置的方法可以满足您的需求。

string[] lines = System.IO.File.ReadAllLines(@"C:sample.txt");

如果您想逐行读取文件

List<string> lines = new List<string>();
using (StreamReader reader = new StreamReader(@"C:sample.txt"))
{
while (reader.Peek() >= 0)
{
string line = reader.ReadLine();
//Add your conditional logic to add the line to an array
if (line.Contains(searchTerm)) {
lines.Add(line);
}
}
}

您可以使用的另一个选项是读取每一行,同时将行拆分为段,并仅将第一个元素与提供的搜索项。我在下面提供了一个完整的工作演示:

解决方案:

class Program
{
static void Main(string[] args)
{
// Get all lines that start with a given word from a file
var result = GetLinesWithWord("The", "temp.txt");
// Display the results.
foreach (var line in result)
{
Console.WriteLine(line + "r");
}
Console.ReadLine();
}
public static List<string> GetLinesWithWord(string word, string filename)
{
List<string> result = new List<string>(); // A list of strings where the first word of each is the provided search term.
// Create a stream reader object to read a text file.
using (StreamReader reader = new StreamReader(filename))
{
string line = string.Empty; // Contains a single line returned by the stream reader object.
// While there are lines in the file, read a line into the line variable.
while ((line = reader.ReadLine()) != null)
{
// If the line is white space, then there are no words to compare against, so move to next line.
if (line != string.Empty)
{
// Split the line into parts by a white space delimiter.
var parts = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
// Get only the first word element of the line, trim off any additional white space
// and convert the it to lowercase. Compare the word element to the search term provided.
// If they are the same, add the line to the results list.
if (parts.Length > 0)
{
if (parts[0].ToLower().Trim() == word.ToLower().Trim())
{
result.Add(line);
}
}
}
}
}
return result;
}
}

示例文本文件可能包含的位置:

在这个保持着
死者灵魂的领域里,我该如何认识你,
当你们所有人的时间都可以枯萎、沉睡
并在我们踩过的尘土中死去时

因为我将感受到无休止的疼痛的刺痛
如果我不在那里遇见你温柔的存在
再也听不到我爱的声音,再也读不到
在你宁静的眼睛里温柔的思想

难道你那颗温柔的心不要求我去那里吗
那颗最让我悸动的心
我在地上的名字曾在你的祈祷中出现过,
它会在天堂从你的舌头上被放逐吗

在天堂呼吸生命的风吹拂下的草地上,在那辉煌的领域里,在那无拘无束的心灵的更大的运动中,
你会忘记与我们在一起的爱吗

经历了所有风雨的过去的爱,
温柔地忍受着我更严厉的本性,

越深越深,直到最后,
它会随着生命而终止吗

比我更快乐的命运,更大的光芒,
在那里等待你;因为你顺从了你的意愿
以愉快的方式向正义的规则致敬,
爱所有人,以德报怨。

对我来说,我所居住的肮脏的关怀,
收缩并消耗我的心,就像加热卷轴一样
愤怒留下了它的伤疤——地狱之火在我的灵魂上留下了可怕的伤疤

然而,尽管你戴着天空的荣耀,
你难道不保持同样的名字吗

在那平静的家里,你难道不教我
我在这件事上学到的智慧——
爱的智慧——直到我成为
你在那幸福之地的合适伴侣吗

您想通过调用这样的方法来检索每一行,其中该行的第一个单词是单词"the":

var result = GetLinesWithWord("The", "temp.txt");

您的结果应该如下:

死者的灵魂,
经历了所有暴风雨过去的爱,
同一个美丽体贴的额头和温柔的眼睛,
我在这件事上学到的智慧——
爱的智慧——直到我成为

希望这能充分回答你的问题。

最新更新