-1

I have the following problem with this .NET C# application.

I have this string array:

string[] uorsList = uors.Split(';');

Sometimes this array contains an element corresponding to the empty string ("").

What is a smart way to remove all the element that are empty string from this uorsList array?

4
  • 7
    Try doing: string[] uorsList = uors.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries); Commented May 20, 2019 at 9:38
  • 1
    In the general sense, how to filter an array to leave out empty string values, you can use the answer posted by Oliver, but in this particular case, it is better using the code @KunalMukherjee posted in his comment because this will ensure the empty strings are not added to the array in the first place. Commented May 20, 2019 at 9:40
  • I find it interesting how quickly people rush to produce a LINQ answer when a very simple solution using string.Split is already available... Commented May 20, 2019 at 9:52
  • @Martin, given the context of the question pertains to removing items from the array, LINQ is a perfectly valid option - even if it isn't the best option for this example. Based on the title and the content of the question, users may reach this page when they haven't used string.Split. Commented May 20, 2019 at 10:00

3 Answers 3

8

You can use LINQ to filter out the empty entires:

using System.Linq;
...

string[] uorsList = uors.Split(';');
var filtered = uorsList.Where(s=> !string.IsNullOrWhiteSpace(s)).ToArray();

EDIT:

As pointed out in comments, when using string.Split the following is a better option as the empty entries will never make it into the array:


string[] uorsList = uors.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries);

Sign up to request clarification or add additional context in comments.

5 Comments

Better to ensure the empty strings doesn't end up in the array in the first place using the code posted in the comments by Kunal.
You can aply directly string[] uorsList = uors.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
@LasseVågsætherKarlsen that is true in this case. I'll leave this here for anyone that finds this page and hasn't used string.Split
Agreed, for a general issue not related to String.Split, this is an answer. Perhaps you should edit in the StringSplitOptions variant in your answer as well?
Thanks, I've added an edit
1

These are some ways to do it:

string uors = ";bla;bla;";
string[] uorsList = uors.Split(';').Where(x => string.IsNullOrEmpty(x) == false).ToArray();

string uors = ";bla;bla;";
string[] uorsList = uors.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries);

string uors = ";bla;bla;";
List<string> uorsList = uors.Split(';').ToList();
uorsList.RemoveAll(x => string.IsNullOrEmpty(x));

Comments

1

You can try the following :

string[] uorsList = uors.Split(';').Where(s => s != string.IsNullOrWhiteSpace).ToArray();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.