Click here to Skip to main content
15,887,430 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
Hi there
I Have

string[] myArray=new []{"1","2","3",""};

How can i convert/Format myArray to Look Like

string[] myArray=new []{"1","2","3"};

In other works, how can i programmatically remove the last empty entry

I am assuming that i don't know the index of the empty entry.

Thanks in advance

Martin
Posted
Updated 19-Mar-19 19:37pm
v2
Comments
BillWoodruff 22-Jan-12 10:53am    
Is it absolutely essential you must use an array of strings here ?
Sergey Alexandrovich Kryukov 22-Jan-12 17:35pm    
You are right if you mean it does not have to be an array -- could be too expensive in terms of performance.
That's why I added my answer, please see.
--SA

you can do this easy ans simple way

myArray = myArray.Where(x => !string.IsNullOrEmpty(x)).ToArray();
 
Share this answer
 
Comments
Richard Deeming 21-Mar-19 11:17am    
It might be simpler, but it will have significantly worse performance than solution 1 for large arrays.

Both ToList and ToArray have optimizations in place when the input implements ICollection<T>. But the sequence returned from Where doesn't implement ICollection<T>, so ToArray has to start with an array of 4 elements; each time it reaches the end, it creates a new array twice the size, and copies the old array to it; and finally, it has to create another new array of the correct size, and copy the elements to that.
Member 14363752 11-May-19 10:28am    
I had to join Code Project just to commend you!

I have a very large, variable in size, string array, that is written to a file in a direct deposit banking project. I now use your solution as an extra error handling mechanism.
Thanks.
C# doesn'ty have a direct way to do that, but... you could try:
C#
string[] x = new string[] { "1", "", "2", "", "3", "4" };
List<string> y = x.ToList<string>();
y.RemoveAll(p => string.IsNullOrEmpty(p));
x = y.ToArray()
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Jan-12 17:30pm    
Simple compact way. but the performance toll of using array can be a problem.
This is the same as creation of a brand new array and copying of all the remaining data.

OP might need to think about using collections.
My 5 anyway.

--SA
Sergey Alexandrovich Kryukov 22-Jan-12 17:34pm    
So, I added an alternative answer...
--SA
I would advise you to review your design taking into account the inherent cost of removal or insertion of an element from/into the array. This is the same as creation of a brand new array and making a copy of all data. The performance can suffer too much. Using a list as a temporary object won't solve the problem.

So, consider using containers in first place. You can use, say System.Collections.Generic.List<>, please see http://msdn.microsoft.com/en-us/library/system.collections.generic.aspx[^].

—SA
 
Share this answer
 
v2
Comments
Amir Mahfoozi 23-Jan-12 0:32am    
+5
Sergey Alexandrovich Kryukov 23-Jan-12 0:54am    
Thank you, Amir.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900