Use split in C #

Source: Internet
Author: User
1. Separated by strings:
Using system. Text. regularexpressions;
String STR = "aaajsbbbjsccc ";
String [] sarray = RegEx. Split (STR, "JS", regexoptions. ignorecase );
Foreach (string I in sarray) response. Write (I. tostring () + "<br> ");
Output result:
Aaa
Bbb
CCC
2. Separate multiple characters:
String STR = "aaajbbbscccjdddseee"; string [] sarray = Str. Split (New char [2] {'J','s '});
Foreach (string I in sarray) response. Write (I. tostring () + "<br> ");
Output result:
Aaa
Bbb
CCC
Ddd
Eee
3. Separate them with a single character:
String STR = "aaajbbbjccc ";
String [] sarray = Str. Split ('J ');
Foreach (string I in sarray) response. Write (I. tostring () + "<br> ");
Output result:
Aaa
Bbb
CCC

//////////////////////////////////////// /// // String [] arr = Str. split ("O ");

This is a statement with syntax errors. The split separator parameter should be char [] or string [], and should not be a string. Correct example:

String STR = "technology ";
Char [] separator = {'O '};
String [] arr = Str. split (separator ); //////////////////////////////////////// //// // string. the split method has six overload functions:

Program code 1) Public String [] Split (Params char [] separator) 2) Public String [] Split (char [] separator, int count)
3) Public String [] Split (char [] separator, stringsplitoptions options)
4) Public String [] Split (string [] separator, stringsplitoptions options)
5) Public String [] Split (char [] separator, int count, stringsplitoptions options)
6) Public String [] Split (string [] separator, int count, stringsplitoptions options) Below we use some instances to explain how to use (the following string words = "1, 2.3 ,, 4 ";):
1. Public String [] Split (Params char [] separator)Program code string [] split = words. split (New char [] {','}); // return value: {"1", "2.3", "", "4 "}
String [] split = words. split (New char [] {',', '. '}); // return: {"1", "2", "3", "", "4 "}
2. Public String [] Split (char [] separator, int count)Itpub personal space, N: H! C0m/s3u \ u0002p program code string [] split = words. split (New char [] {',', '. '}, 2); // return: {"1", "2.3, 4 "}
String [] split = words. split (New char [] {',', '. '}, 6); // return: {"1", "2", "3", "", "4 "}
3. Public String [] Split (char [] separator, stringsplitoptions options)Program code string [] split = words. split (New char [] {',', '. '}, stringsplitoptions. removeemptyentries); // return value: {"1", "2", "3", "4"} empty element not retained
String [] split = words. split (New char [] {',', '. '}, stringsplitoptions. none); // return: {"1", "2", "3", "", "4"} retain null Elements
4. Public String [] Split (string [] separator, stringsplitoptions options)Program code string [] split = words. split (New String [] {",", ". "}, stringsplitoptions. removeemptyentries); // return value: {"1", "2", "3", "4"} empty element not retained
\ U0002w1i + CH % ^ \ u0017} 0 string [] split = words. split (New String [] {",", ". "}, stringsplitoptions. none); // return: {"1", "2", "3", "", "4"} retain null Elements
5. Public String [] Split (char [] separator, int count, stringsplitoptions options)Program code string [] split = words. split (New char [] {',', '. '}, 2, stringsplitoptions. removeemptyentries); // return: {"1", "2.3, 4"} do not retain the empty element itpub personal space 1 K; e \ u0007f \ u0008f} \ u0011c n
String [] split = words. split (New char [] {',', '. '}, 6, stringsplitoptions. none); // return: {"1", "2", "3", "", "4"} retain null Elements
6. Public String [] Split (string [] separator, int count, stringsplitoptions options)Program code string [] split = words. split (New String [] {",", ". "}, 2, stringsplitoptions. removeemptyentries); // return value: {"1", "2.3, 4"} empty elements are not retained.
String [] split = words. split (New String [] {",", ". "}, 6, stringsplitoptions. none); // return: {"1", "2", "3 ","", "4"} to retain null elements, note that the Public String [] Split (string [] separator) function is not overloaded, so we cannot be like VB. net. split (","), but can only use words. split (',')

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.