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

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: