JG Vimalan's Blog

It is all about coding!

Remove empty strings, spaces and \r\n in string array

In this code sample, I am going to show how to remove empty string, newline characters in string array.

I have a string array as shown below and used StringSplitOptions.RemoveEmptyEntries to remove any empty elements in the array.

string [] clinics = ConfigurationManager.AppSettings[“MyClinicList”].Split(newchar[] {‘,’}, StringSplitOptions.RemoveEmptyEntries);

Now, I found that, the clinics array elements are having white spaces \r\n due to newline. So, used Array.ForEach and Trim method as shown below,

Array.ForEach<string>(clinics, val => clinics[Array.IndexOf<string>(clinics, val)] = val.Trim());

This gave the expected result.


May 18, 2012 - 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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: