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 examples to illustrate 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 value: {"1", "2", "3", "", "4 "}
2. public string [] Split (char [] separator, int count)ITPUB personal space, n: H! C0M/S3Uu0002P 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", & q

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.