RemoveEmptyEntries not working?

I have tried alot of things to get this working but everything I try will compile but will not work.

			char[] charDataPairSplits = new char[]
				{
					':',
					','
				};
			char[] charDataLineSplits = new char[]
				{
					'\n'
				};
				
			strFileText = File.ReadAllText(strDocumentsPath+fileName); 
			strDataLines = strFileText.Split(charDataLineSplits, StringSplitOptions.RemoveEmptyEntries);        
			strDataPairs = new string[strDataLines.Length][];
			
			int lineNum = 0;
			foreach (string line in strDataLines)        
			{ 
				if(line != " ")
				{
					strDataPairs[lineNum++] = line.Split(charDataPairSplits); 
				}
			}

my file looks like this…

Name01, true: Null text goes here 001
Name02, false: Null text goes here 002

Name03, null: Null text goes here 003

So my problem is since there are 4 lines in the text file my strDataPairs[2][0] returns nothing as in “” while strDataLines[2] returns “” as well.

It is not removing the empty line. So what am I doing wrong?

Maybe it’s including the carriage return character “\r”? Instead of File.ReadAllText you can use File.ReadAllLines and/or use the String.Trim() method:

string lines[] = File.ReadAllLines(strDocumentsPath + fileName);
int lineNum = 0;
List<string[]> strDataPairs = new List<string>();
foreach (string line in lines)
{
	string trimmed = line.Trim();
	if (trimmed != String.Empty)
	{
		strDataPairs.Add(trimmed.Split(charDataPairSplits));
	}
}

pseudo-code, off the top of my head, untested, etc.

yeap … added ‘\r’ to my charDataLineSplits array and now it works. thanks! Now I have to runs some tests to figure out what \r and \n do.

EDIT:

Should I do this?

         char[] charDataLineSplits = new char[] 
            { 
               '\n'
               '\r'
               '\r\n' 
            };

I would imagine internally the method splits in the same order as the array, so maybe put the \r\n first to make it a tad more efficient. But honestly, it should be fine the way it is.

EDIT: you should be fine simply by having the two entries ‘\r’ and ‘\n’; you shouldn’t need the third ‘\r\n’ entry.