I have a string array. I need to remove some items from that array. but I don't know the index of the items that need removing.
My array is : string[] arr= {" ","a","b"," ","c"," ","d"," ","e","f"," "," "}.
I need to remove the " " items. ie after removing " " my result should be arr={"a","b","c","d","e","f"}
how can I do this?
string[] arr = {" ", "a", "b", " ", "c", " ", "d", " ", "e", "f", " ", " "};
arr = arr.Where(s => s != " ").ToArray();
This will remove all entries that is null, empty, or just whitespaces:
arr.Where( s => !string.IsNullOrWhiteSpace(s)).ToArray();
If for some reason you only want to remove the entries with just one whitespace like in your example, you can modify it like this:
arr.Where( s => s != " ").ToArray();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With