Use System. stringsplitoptions to remove null items in the string to be split

Source: Internet
Author: User
Something very simple. I learned it today and wrote it down.

When we use a character to split a string into a string array, if there is an item in the string to be split, that is, "", it will also appear in the result array. If you do not want to include null items in the result array, you can use a new enumerated stringsplitoptions in. NET 2.0. DetailsCodeAs follows:

String S =   " 0, 1, 2, " ;
String [] Res = S. Split ( New   Char [] {','} , Stringsplitoptions. removeemptyentries );
Foreach ( String Str In Res)
{
Console. writeline (STR );
}
Console. writeline ( " Total: {0} " , Res. Length );
Console. Readline ();

The result is:
0
1
2
Total: 3

If you change stringsplitoptions. None or you do not have this parameter (the default value is none), the result is:
0
1
2

Total: 4

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.