Reputation: 2980
I have a string,
string aString = "a,aaa,aaaa,aaaaa,,,,,";
Where i want to insert to a List..But when i do using the following method,
List<string> aList = new List<string>();
aList.AddRange(aString.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries));
MessageBox.Show(aList.Count.ToString());
I get the count as only 4, But there are actually 8 elements even the final characters in between the (,) sign is blank.
But if i pass the string as shown below,
string aString = "a,aaa,aaaa,aaaaa, , , , ,";
It will be shown as 8 elements..Please help me on this, the default way thw program retrieves the string is like so,
a,aaa,aaaa,aaaaa,,,,,
Please help on this one, It would be great if i could add spaces to the empty area or any other way so that i could add all these characters in between (,) sign to the list.. even the blank areas. Thank you :)
Upvotes: 0
Views: 1616
Reputation: 11
I think you must remove StringSplitOptions.RemoveEmptyEntries
aList.AddRange(aString.Replace(",,", ", ,").Split(new string[] { "," }));
Upvotes: 1
Reputation: 5715
You can just Replace
the space before split it.
aList.AddRange(aString.Replace(" ", "").Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries));
Upvotes: 0
Reputation: 116108
Don't use StringSplitOptions.RemoveEmptyEntries
string aString = "a,aaa,aaaa,aaaaa,,,,,";
var newStr = String.Join(", ", aString.Split(','));
Upvotes: 5