In C #, how do I exclude, filter, clear, and delete null strings from the string array,
In C #, how can I delete an empty string from a string array? As Microsoft continues to develop and update C #, the Array Operations in C # become more and more diversified. In the past, to filter empty strings in an array, we had to use loops to exclude and filter them. C #3.0 the newly added lambda expression can easily implement this function. The following describes multiple methods to clear empty strings in the array and in.. net removes the elements whose strings are null from the string array.
Method 1: filter out empty strings in the Array Using lambda expressions
/// <Summary>
/// Use lambda expressions to exclude/filter/clear/delete null strings in the string array
/// </Summary>
/// <Param name = "args"> </param>
Static void Main (string [] args)
{
String [] strArray = {"", "111", "", "222", "", "333 "};
Console. WriteLine ("output array with Null String :");
Foreach (string str in strArray)
{
Console. WriteLine (str );
}
Console. WriteLine ("-------------------------------------------");
// Use lambda expressions to filter out empty strings
StrArray = strArray. Where (s =>! String. IsNullOrEmpty (s). ToArray ();
Console. WriteLine ("output filtered empty string array :");
Foreach (string str in strArray)
{
Console. WriteLine (str );
}
Console. Read ();
}
Click this link to view the official description of Lambda expressions on MSDN.
Method 2: Use the ForEach loop of the generic set List <string> to filter and obtain the correct string and add it to the new string array.
String [] strArray = {"", "111", "", "222", "", "333 "};
Console. WriteLine ("output array with Null String :");
Foreach (string str in strArray)
{
Console. WriteLine (str );
}
Console. WriteLine ("-------------------------------------------");
/*
* Use the ForEach method of the List generic set to obtain non-empty strings cyclically
* The anonymous method is used here.
*/
List <string> list = new List <string> ();
StrArray. ToList (). ForEach (
(S) =>
{
If (! String. IsNullOrEmpty (s ))
{
List. Add (s );
}
}
);
StrArray = list. ToArray ();
Console. WriteLine ("output filtered empty string array :");
Foreach (string str in strArray)
{
Console. WriteLine (str );
}
Console. Read ();
Click this link to view the MSDN official description of the List. ForEach method.
Method 3: eliminate and delete null strings in the string array using the traditional loop method
String [] strArray = {"", "111", "", "222", "", "333 "};
Console. WriteLine ("output array with Null String :");
Foreach (string str in strArray)
{
Console. WriteLine (str );
}
Console. WriteLine ("-------------------------------------------");
// Use the loop to exclude and filter out empty strings
List <string> list = new List <string> ();
Foreach (string s in strArray)
{
If (! String. IsNullOrEmpty (s ))
{
List. Add (s );
}
}
StrArray = list. ToArray ();
Console. WriteLine ("output filtered empty string array :");
Foreach (string str in strArray)
{
Console. WriteLine (str );
}
Console. Read ();