How to create an Array from external string file

前端 未结 1 1582
庸人自扰
庸人自扰 2021-01-22 17:42

How add add array in Speech recognition program see below code ? I tried use streamRead read a string and make a array and put behind commands.Add(new String[]

相关标签:
1条回答
  • 2021-01-22 18:12

    How to make an array by importing strings from a file ?

    This will give you all the lines from a file:

    var lines = File.ReadAllLines(@"PathToYourFile");
    

    The above reads all lines from the file into memory. There is another method which will read the lines one by one as you need them:

    var lines = File.ReadLines(@"PathToYourFile");
    

    This one returns IEnumerable<string>. For example, lets say your file has 1000 lines, ReadAllLines will read all 1000 lines into memory. But ReadLines will read them 1 by 1 as you need them. Therefore, if you do this:

    var lines = File.ReadLines(@"PathToYourFile");
    var line1 = lines.First();
    var lastLine = lines.Last();
    

    It will only read the first and the last line into memory even though your file has 1000 lines.

    So when to use the ReadLines method?

    Let's say you need to read a file which has 1000 lines and the only lines you are interested in reading are 900 to 920, then you can do this:

    var lines = File.ReadLines(@"PathToYourFile");
    var line900To920 = lines.Skip(899).Take(21);
    
    0 讨论(0)
提交回复
热议问题