ISHIDA ISHIDA -4 years ago 102
C# Question

split a string based on white space in c#

I have a string "dexter is good annd bad".

I want create a list by splitting this string based on the space.

I have achieved this using following code

string ss = "dexter is good annd bad";
var s = !string.IsNullOrEmpty(ss) && ss!= "null"? ss.Split(' ').ToList(): new List<string>();

Problem is this list also contains spaces, I don't need spaces or empty string to be in my list.

Can anyone help me with this.

Thank you

Answer Source

You can use String.Split method:

var s = ss.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download