JG Vimalan's Blog

It is all about coding!

Remove empty values in string array

I got a task where I need to read values from a text file which are separated by line break \r\n.

When I read the file using C#, I found empty entry after each element with value. Here is the code block,

TextReader fileReader = new StreamReader(@”C:\members.txt”);

string fileText = fileReader.ReadToEnd();

char[] newLineChar = Environment.NewLine.ToCharArray();

string[] memberIds = fileText.Split(newLineChar, StringSplitOptions.RemoveEmptyEntries);

In order to remove the empty elements, in the Split method, I passed the  StringSplitOptions.RemoveEmptyEntries, which gave the desired result.

Advertisements

October 10, 2011 Posted by | C#.NET | Leave a comment