Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>();

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

like image 779
ISHIDA Avatar asked Oct 12 '25 19:10

ISHIDA


1 Answers

You can use String.Split method:

var s = ss.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
like image 130
Darjan Bogdan Avatar answered Oct 14 '25 09:10

Darjan Bogdan