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: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.
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:4
test = test.Where(x => !string.IsNullOrWhitepace(x)).ToArray();
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.
Source: stackoverflow.com
Related Articles
- How remove empty element from string array in one line?
- Remove all empty elements from string array
- Remove Empty Element using XDocument
- 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?
- List or Array of String Contain specific word in Html Source Code
- Remove Element From String Array Using LINQ Contains Value
- 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?
- Initializing a C# array with multiple copies of the same element
- 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 - array property contains element from another array
- LINQ: How to remove element from IQueryable<T>
- linq remove item from object array where property equals value
- LINQ to return null if an array is empty
- Select every second element from array using lambda
- LINQ: Remove Element from XML based on attribute value?
- 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?
- Using LINQ to delete an element from a ObservableCollection Source
- LINQ Source Code Available
- C# LINQ: remove null values from an array and return as not nullable
- How to .ToUpper() each element of an array using LINQ?
- Using Linq is there a way to perform an operation on every element in an Array
- Most efficient way of getting the N last element of an array
- How to remove a List string element if it contains a string element from another List?
- .NET 4 Code Contracts: "requires unproven: source != null"
- remove all element except from given index number
- Nullable Expressions
- C# for each property get both value and name
- Dynamic JSON querying JSON.NET
- LINQ - FirstOrDefault() then Select()
- How can I bind group by query to DataGridview in linq?
- query xml string using LINQ
- C# Compare two strongly typed list
- Comparing two datasources in C#
- Debugging a tough LINQ-to-SQL Timeout
- LINQ - Group By - IEnumerable return
- LINQ - get max number to insert a new record
- How to return a join statement in LINQ
- different results between Linq and SQL
- List<int> except(AnotherList<int>) not return correct results
- C# List Comprehensions = Pure Syntactic Sugar?
- Update IQueryable result before using as join in next query
- Remove duplicates in the list using linq
- Sorting IQueryable by Aggregate in VB.net
- Linq Entity compare 3 fields with wildcard vb.net
- GridView Query not displaying