开发者

C# Getting Data from .txt File separated with Semicolons

By Example I have a .txt File with this content:

开发者_如何学Python
Hello;Test;StackOverflow;I live here
Hi;NoTest;StackOverflow;I don't live here

and so on..

Now I would like to read the txtFile, but I don't want to read the "whole" line or everthing.. only to the next semicolon.. till the end of a Line..

one StringVariable for one Word in the txtFile but just for one line..

How could I achieve this?


You can read text line by line like this:

var streamReader = new StreamReader(new FileStream("c:\\file.txt"));
var line = streamReader.ReadLine();

var values = line.Split(';');

and then read any value from line like this:

var value = values[2];

And if you want to iterate throught those values you can make it like this:

var streamReader = new StreamReader(new FileStream("c:\\file.txt"));

while(!streamReader.EndOfStream)
{
    var line = streamReader.ReadLine()
    var values = line.Split(';');
    for(var i = 0; i < line.Length; i++)
        Console.WriteLine(values[i]); //example usage
}

streamReader.Dispose();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜