[英]Text file line by line into string array
我需要帮助,尝试获取一个大文本文档〜1000行并将其逐行放入字符串数组。
例:
string[] s = {firstLineHere, Secondline, etc};
我还想找到一种方法来查找该行的第一个单词,仅找到该行的第一个单词,一旦找到第一个单词,就复制整个行。 仅查找第一个单词或每一行!
有一种内置方法可以满足您的要求。
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);
}
}
}
您可以使用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();
这将为您提供文档中所有行的数组,这些行以您在标识符字符串数组中定义的关键字开头。
您可以使用的另一种选择是读取每行,同时将行拆分为段,然后仅将第一个元素与提供的搜索词进行比较。 我在下面提供了完整的工作演示:
解:
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;
}
}
示例文本文件可能包含:
我怎么会知道你在这个领域
死者无形的灵魂,
当你所有的时间都可以凋谢时
并在我们踩踏的尘土中灭亡?
因为我会感到无休止的痛苦
如果在那里,我不会遇到你的温柔;
不听我爱的声音,也不重读
在你最大的眼神里,温柔的想法。
你自己温柔的心会不会要求我在那里?
那颗最fon动我的心得到了?
我在地上的名字曾在你的祈祷中,
难道要从你的舌头上把它放逐吗?
在被天堂的生命之风扇动的草地上,
在那个辉煌的领域,
不受束缚的思想的更大运动,
您会忘记在这里加入我们的爱吗?
经历了整个风雨历程的爱情,
我那残酷的天性让我温柔地感到厌倦,
越来越深,直到最后,
它会随着生命而终止吗?
比我的快乐得多,光线更大,
在那等你 因为你屈服了你的意志
为了表示对法治的崇高敬意,
最爱所有人,为患病做好准备。
对我来说,我所住的肮脏的关心,
收缩并消耗我的心,就像加热卷轴一样;
愤怒已留下疤痕-地狱之火
给我的灵魂留下了可怕的伤痕。
尽管你穿上了天空的荣耀,
你不会保留相同的心爱名字,
一样的周到的额头和温柔的眼睛,
在天堂宜人的气候中,Lovelier还一样吗?
在那平静的家中,你不可教我
我从中学到的智慧很糟糕
直到我成为爱的智慧
在那片幸福的土地上,您合适的伴侣?
您想通过调用如下方法来检索每一行中第一个单词为单词“ the”的行:
var result = GetLinesWithWord("The", "temp.txt");
您的结果应为以下内容:
死者无形的灵魂,
经历了整个风雨历程的爱情,
一样的周到的额头和温柔的眼睛,
我从中学到的智慧很糟糕
直到我成为爱的智慧
希望这足以回答您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.