开发者

c# starting from second element in var

开发者 https://www.devze.com 2023-02-01 01:38 出处:网络
var lines = File.ReadAllLines(f开发者_如何学编程ilelocation); char[] space = { \',\' }; string templine;
var lines = File.ReadAllLines(f开发者_如何学编程ilelocation);
                char[] space = { ',' };
                string templine;
                foreach (string line in lines)
                {}

how do i do foreach (string line in lines[1:]) ? i want to skip the first element and start foreach from the second one


If you are targeting .NET 3.5 or above, LINQ:

foreach (string line in lines.Skip(1)) {...}

Although it there is a lot of data, a line-reader may be better, to avoid having to hold all the lines in memory:

public static IEnumerable<string> ReadLines(string path) {
    using(var reader = File.OpenText(path)) {
        string line;
        while((line = reader.ReadLine()) != null) {
            yield return line;
        }
    }
}

then:

foreach(var line in ReadLines(filelocation).Skip(1)) {...}
0

精彩评论

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

关注公众号