I have text file in a following format
432
23 34 45 56 78 90
67 87 90 76 43 09
.................
I want to remove first line and insert rest of the words into an array that separated from white space.
I wrote following code to get words by removing white 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)
once I put a debug point on string[] splitwords
line i can see following out put
How can I remove first line and get rest of the words from array index [0]
?
You need to split with all whitespaces, not just a plain space.
Use @"\s+"
pattern to match 1+ whitespace symbol(s):
string[] splitwords = Regex.Split(stringWithMultipleSpaces, @"\s+");
Another approach is reading the file line by line, and - if there are always only numbers like those and no Unicode spaces present - use plain String.Split()
.
Something like
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());
}
}