Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how would you remove the blank entry from array

Tags:

c#

How would you remove the blank item from the array?

Iterate and assign non-blank items to new array?

String test = "John, Jane";

//Without using the test.Replace(" ", "");

String[] toList = test.Split(',', ' ', ';');
like image 998
Rod Avatar asked Jan 25 '11 21:01

Rod


People also ask

How do I remove blank strings from a list?

Method #1: Using remove() This particular method is quite naive and not recommended use, but is indeed a method to perform this task. remove() generally removes the first occurrence of an empty string and we keep iterating this process until no empty string is found in list.

How do you remove an array from a key?

Using unset() Function: The unset() function is used to remove element from the array. The unset function is used to destroy any other variable and same way use to delete any element of an array. This unset command takes the array key as input and removed that element from the array.

How do I remove all items from an array?

Use the Array. splice() method to remove all the elements from the original array. Use the Array. pop() method with a loop to remove all the elements of the original array.

What is a blank array?

An empty array is an array of length zero; it has no elements: int[] emptyArray = new int[0]; (and can never have elements, because an array's length never changes after it's created).


1 Answers

Use the overload of string.Split that takes a StringSplitOptions:

String[] toList = test.Split(new []{',', ' ', ';'}, StringSplitOptions.RemoveEmptyEntries);
like image 62
Tim Lloyd Avatar answered Oct 27 '22 18:10

Tim Lloyd