score:0
Could the problem be that you are searching for white space instead of an empty string?
Try below:
for(int i = 0; i < allToAddresses.Length; i++)
{
if(allToAddresses[i] == "") // find where empty element is
{ //Here i am trying to delete that empty element. does not work
allToAddresses[i].Split("".ToCharArray(),StringSplitOptions.RemoveEmptyEntries);
}
}
score:1
If you are using arrays, you will need to pull the valid values out and put them into a new instance of an array. You can do something like this:
internal static T[] RemoveNullArrayElements<T>(T[] array)
{
if (array != null)
{
List<T> newLst = new List<T>();
foreach (var ar in array)
{
if (ar != null)
{
newLst.Add(ar);
}
}
return newLst.ToArray();
}
return array;
}
score:2
You cannot truly "remove" elements from an array, because array size is fixed*. You can, however, construct a new array that skips all empty elements:
allToAddresses = allToAddresses.Where(s => !string.IsNullOrWhiteSpace(s)).ToArray();
The above requires using System.Linq
at the top of your file. It checks all entries in your array to see if they are null or consist entirely of white space (spaces, tabs, etc.) and produces a new array of strings, containing only non-empty / non-null entries from the original array.
* In the interest of full disclosure, .NET does have an API that lets you modify array size, but you should not use it in situations like this.
score:4
test = test.Where(x => !string.IsNullOrWhitepace(x)).ToArray();
score:7
You could try to use Linq for this
allToAddresses = allToAddresses.Where(address=>!string.IsNullOrWhiteSpace(address))
.ToArray();
You have to include also this in your namespaces:
using System.Linq;
You filter your initial array using the Where
method. In this method you pass a predicate that returns true if for the current address the method string.IsNullOrWhiteSpace returns false. Otherwise it returns false. Using this filter you discard the addresses that are null, empty, or consisted only of white-space characters.
Source: stackoverflow.com
Related Query
- How remove empty element from string array in one line?
- How do I remove an empty element in an array?
- how to remove repeating elements in an array using LINQ?remove an element if its repeating?
- How can I use linq to remove a certain string from an array while it exists as the first or last element
- how to remove empty strings from list, then remove duplicate values from a list
- How to remove the first element in an array?
- Remove all empty elements from string array
- How to count the number of code lines in a C# solution, without comments and empty lines, and other redundant stuff, etc?
- How to remove an element from an xml using Xdocument when we have multiple elements with same name but different attributes
- LINQ: How to remove element from IQueryable<T>
- How to query if array is null or empty using MongoDB and C# Driver?
- How to remove first occurence of element in List C# with LINQ?
- How to .ToUpper() each element of an array using LINQ?
- How to remove a List string element if it contains a string element from another List?
- Remove Empty Element using XDocument
- How to get the value of an XML element using Linq even when empty
- How to remove duplicates from an Array using LINQ
- How to Remove elements from one List based another list's element and condition?
- How to check if a char array contains every element in another char array?
- How to remove a subarray of an array
- How can I make my procedure for finding the Nth most frequent element in an array more efficient and compact?
- How can i remove the duplicate element from a xml document without scribbling the structure of xml using c#
- How to find an element in nested child array document in MongoDB using c#
- How to find the index of an element in an array inside a lambda expression in c#
- How do I get first element of each array via Linq C#
- How to check if one Element of an array matches or a part of an element of another array in c#?
- How to remove from LINQ results, matching elements from array
- LINQ How to Remove Empty Elements at End of Collection
- How to check the second element of an array in linq only if it exists
- How do I remove a particular element without deleting its child or data in C#?
More Query from same tag
- Performance tuning Entity Framework queries
- Convert one JSON structure to another using Json.Net
- Reverse of Expression<Func<T,TResult>>.Compile()?
- NHibernate 3 - extending Linq provider BaseHqlGeneratorForMethod.BuildHql problem
- Comparing two Lists and returning the distinct values and the differences
- LINQ String Array Not IN
- group a table list by a column name (sample included)
- Find missing items in a List<T>
- Calculated field in model class gives exception
- Join list into string
- C# search query for comma delimited access database field
- Convert to dot notation
- Linq Elements Attribute string list
- How to include date criteria in RavenDB Lucene query
- How can I write list.GroupBy(x => x.AccountNumber).Select(g => g.First()) without LINQ
- How to generate generic GetById(int Id) function in Linqtosql?
- how to subtract two datatables with linq
- How to check if array values exist in another table inside my application?
- Create list of tuples with data from nested lists with Linq
- Getting around lack of 'Contains' in Linq To Entities
- Filter Both Parent And Child Collection
- What is causing this LINQ to XML Argument Exception?
- Linq Include property in every item of the list
- GroupBy on Records considering the highest value on a different column
- Get All Property of a specific class with one property of parent
- Linq query and group by data from table which has one to many relationship?
- CreateDocumentQuery() with property other than id
- DataGridView AddRange, Cast IEnumerable<object[]> to DataGridViewRow[] (If possible, using LINQ)
- C# LINQ Multiple GroupJoin using Lambda Syntax
- Create a FSharpMap (of FSharpMap) using GroupBy in LINQ