根据起始索引和结束索引从.txt中读取行。 [英] Read lines from a .txt based on start index and end index .

查看:114
本文介绍了根据起始索引和结束索引从.txt中读取行。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文档,它包含像



这样的行问题步骤:7 asdasfdgf ..... {我在这里可以找到总步数}



[步骤从这里开始]



问题第1步:aaaaaaaaa

问题第2步:bbbbbbbb

问题第3步:cccccccc

问题第4步:ddddddd

问题第5步:eeeeeee

问题第6步:ffffffff









问题步骤n:nnnnnnnnn



如何根据步骤总数从文本文档中逐行读取并保存在列表或数组中



提前致谢

解决方案

您可以使用System.File [ ^ ]类。例如,它有一个名为ReadAllLines()的方法。



只需阅读它们并做你需要做的事。


< blockquote>像这样:

 List< string> dataList =  new  List< string>(); 

使用(StreamReader sr = new StreamReader( data.txt))
{
while (sr.Peek()> 0
{
dataList.Add(sr.ReadLine());
}

while (sr.Peek()> 0
{
string temp = sr.ReadLine();

if (temp.StartsWith( Problem Step))
{
dataList.Add(temp);
}
}
}


I have a text document and it contain lines like

Problem Steps: 7 asdasfdgf.....{Here i can find total number of steps}

[steps starts here ]

Problem Step 1: aaaaaaaaa
Problem Step 2: bbbbbbbb
Problem Step 3: cccccccc
Problem Step 4: ddddddd
Problem Step 5: eeeeeee
Problem Step 6: ffffffff
.
.
.
.
Problem Step n: nnnnnnnnn

How to read each line by line from the text document based on the total number of steps and save in a list or array

Thanks in advance

解决方案

You can use the System.File[^] class. It has, for example, a method named ReadAllLines().

Just read them in and do what you need to do.


Like this:

List<string> dataList = new List<string>();

using(StreamReader sr = new StreamReader("data.txt"))
{
    while(sr.Peek() > 0)
    {
        dataList.Add(sr.ReadLine());
    }
    while(sr.Peek() > 0)
    {
        string temp = sr.ReadLine();

        if(temp.StartsWith("Problem Step"))
        {
            dataList.Add(temp);
        }
    }
}


这篇关于根据起始索引和结束索引从.txt中读取行。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆