I have a log file with some lines:
test123 (random text..)
test123 (random text..)
test123 (random text..)
I want to convert it into an array, so I did the following:
string[] myArray = logFileText.Replace("test123", "#test123").Split("#");
This work fine except for the fact that the first element in myArray is empty.. any idea how to solve this?
Note: cannot use Environment.NewLine, it does not work on this file for some reason..
CodePudding user response:
You could add a .Skip(1)
to the end of your code:
string[] myArray = logFileText.Replace("test123", "#test123").Split("#").Skip(1).ToArray();
CodePudding user response:
You need to read the file line-by-line. This should work
var filePath = "D:\\MyLogfile.txt";
var fileContents = File.ReadAllLines(filePath); //Reads line by line
if (fileContents.Length > 0) //If file has any lines/content
{
var myFileContents = fileContents.Skip(1); //Skips the first row
foreach (var fileLine in myFileContents) //Process line by line
{
}
}
CodePudding user response:
As @pm100 said, this is the answer:
Split('#', StringSplitOptions.RemoveEmptyEntries)