In C #, how do I exclude, filter, clear, and delete null strings from the string array,

Source: Internet
Author: User

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 ();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.