读取文本文件中的特定行 [英] Read specific line in text file
本文介绍了读取文本文件中的特定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好的,我有一个程序需要从类似这样的文本文件中读取数据
Ok so I've got a program that needs to read from a text file that looks like this
[Characters]
John
Alex
Ben
[Nationality]
Australian
American
South African
[Hair Colour]
Brown
Black
Red
我想做的是只有一种方法可以根据传递的参数读取一个部分.
What I would like to do is only have one method that reads a section depending on the parameter that is passed.
这可能吗?如何实现?
推荐答案
var sectionName = "[Nationality]";
string[] items =
File.ReadLines(fileName) //read file lazily
.SkipWhile(line => line != sectionName) //search for header
.Skip(1) //skip header
.TakeWhile(line => !string.IsNullOrEmpty(line))//take until next header
.ToArray(); //convert to array
items
将有:
Australian
American
South African
这篇关于读取文本文件中的特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文