我需要帮助,试图把一个大的文本文件〜1000行,并将其逐行放入一个字符串数组中。将文本文件逐行写入字符串数组
例子:
string[] s = {firstLineHere, Secondline, etc};
我也想办法找到的第一个字,只是该行的第一个字,而一旦第一个字就发现,复制整个行。只找到第一个字或每一行!
我需要帮助,试图把一个大的文本文件〜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);
}
}
}
之前使用过的最大的文本文件,这种方式不适用于我的搜索,我试图找到第一个单词,一旦找到,将其余的行放入一个字符串。有没有其他方法? –
@The_Reich:那么你应该写一个描述你所需要的问题。这是非常糟糕的形式。 –
@The_Reich请更新您的问题,准确说明您正在尝试做什么,否则您将无法获得所需的答案。 – Tim
您可以File.ReadAllLines一点点的LINQ合并完成这个(以完成除的注释中规定的问题普利文的回答
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;
}
}
当样品文本文件可能包含:
怎能我知道你在保持
球死亡的灵魂,
当所有的时间都可以枯萎睡觉
我们踩在尘土之中吗?
因为我会感到不断的痛苦
如果我遇到你的温柔的存在不是;
也没有听到我爱的声音,也没有再读
在你最温柔的目光中温柔的思想。
你自己温顺的心不会要求我在那里吗?
那个最爱我的心是谁给的?
我在地球上的名字永远在你的祈祷中,
难道它会从你的舌头放逐在天堂吗?
在天上的生命呼吸风拂草地,
在光荣球的辉煌,无拘无束的心灵
和较大的动作,
你愿意忘记,这里加入了我们的爱情?
通过所有的暴风雨过去住的爱,
,怯生生地跟我严厉自然孔,
和更深的增长,投标到最后,
必用的生活到期,并没有更多?
比我的快乐多了,还有更大的光线,
在那里等待着你;因为你已经屈从于你的意愿
在对正义的欢迎致敬中,
并且爱护所有,并且善良的生病。
对我来说,我居住的肮脏的关怀,
收缩和消耗我的心,因为热量的滚动;
愤怒已经留下了它的疤痕 - 地狱之火
已经在我的灵魂上留下了可怕的伤疤。
然而,尽管你wear'st天空的荣耀,
你不再保持相同的可爱的名字,
同样公平的周到额头,温柔的眼睛,
可爱天上的甜蜜气氛,又是同一?
你必不教我,在这平静的家,
,我学会了在this--
智慧病得很智慧,这就是爱 - 直到我成为
在幸福的土地你的伴侣合?
你想获取每一行,其中行的第一个字是“”通过调用像这样的方法的话:
var result = GetLinesWithWord("The", "temp.txt");
你的测试结果则是以下几点:
死者的游魂,
通过所有的暴风雨过去住的爱,
同样公平的你ghtful眉头,又温柔的眼睛,
我在this--
学到了生病智慧是爱智慧 - 直到我成为
希望这回答你的问题充分足够。
这是什么方法可以称为@James Shaw,当我将“单词”设置为我想查找的单词时,它似乎没有返回任何单词! –
@The_Reich我已经更新了我提供的解决方案,以便更清晰。为了测试它,用上面的代码替换你现有的程序类。请让我知道你是否需要任何进一步的帮助。 –
非常感谢! –
'System.IO.File.ReadAllLines()'它不是一行行,但否则应该执行作业 – Plutonix
@PraveenPaulose,在我发布和删除评论后才意识到它。 –
您可以提供更多关于您为什么认为〜1000行是大文档的信息吗?这些线条非常长吗? –