从文本文件中删除第一行并将其余单词放入与空格分隔的数组中,

remove first line from text file and put rest of words into an array that separated from white-spaces,

我有以下格式的文本文件

432 23 34 45 56 78 90 67 87 90 76 43 09 .................

我想删除第一行并将其余单词插入与白色分隔的数组中 space。

我写了下面的代码来通过删除白色来获取单词 spaces

    StreamReader streamReader = new StreamReader("C:\Users\sample.txt"); //get the file
    string stringWithMultipleSpaces = streamReader.ReadToEnd(); //load file to string
    streamReader.Close();

    Regex newrow = new Regex(" +"); //specify delimiter (spaces)
    string[] splitwords = r.Split(stringWithMultipleSpaces); //(convert string to array of words)

一旦我在 string[] splitwords 行上放置了一个调试点,我就可以看到下面的输出

如何删除第一行并从数组索引 [0] 中获取其余单词?

你需要用全白space拆分,而不仅仅是普通的space。

使用@"\s+"模式匹配1+个白色space符号:

string[] splitwords = Regex.Split(stringWithMultipleSpaces, @"\s+");

另一种方法是逐行读取文件,并且 - 如果总是只有这样的数字而没有 Unicode spaces - 使用纯 String.Split().

类似

var results = new List<string>();
using (var sr = new StreamReader("C:\Users\sample.txt", true))
{
    var s  = string.Empty;
    while ((s=sr.ReadLine()) != null)
    {
        results.AddRange(s.Split());
    }
}