Split usage and explanations in C #

Source: Internet
Author: User

The String.Split method provides the following 6 overloaded functions:

name Description

String.Split (char[])
Returns a string array containing the substrings in this instance delimited by the elements of the specified Char array.

Supported by the. NET Compact Framework.

String.Split (char[], Int32) Returns a string array containing the substrings in this instance delimited by the elements of the specified Char array. parameter specifies the maximum number of substrings that are returned.
String.Split (char[], stringsplitoptions) Returns a string array containing the substrings in this string, separated by the elements of the specified Char array. parameter specifies whether an empty array element is returned.
String.Split (string[], stringsplitoptions) Returns a string array containing the substrings in this string, separated by the elements of the specified string array. parameter specifies whether an empty array element is returned.
String.Split (char[], Int32, stringsplitoptions) Returns a string array containing the substrings in this string, separated by the elements of the specified Char array. parameter specifies the maximum number of substrings to return, and whether to return an empty array element.
String.Split (string[], Int32, stringsplitoptions) Returns a string array containing the substrings in this string, separated by the elements of the specified string array. parameter specifies the maximum number of substrings to return, and whether to return an empty array element.

The following details are illustrated below:

1.string.split (char[])

String str = "AAATBBSCCTDD";

string []strarray = str. Split (new char[]{' t '}); or string []strarray = str. Split (' t '); Single character cut (Result: "AAA" "BBSCC" "DD")

string []strarray = str. Split (new char[]{' t ', ' s '}); Multi-byte cut (Result: "AAA" "BB" "CC" "DD")

2.string.split (Char[],int32)

String str = "AAATBBSCCTDD";

string []strarray = str. Split (new char[]{' t ', 2});//Cut only into 2 parts (Result: "AAA" "BBSCCTDD")

3.string.split (char[],stringsplitoptions)

String str = "AAATBBSCCTDDT";

string []strarray = str. Split (new char[]{' t ', stringsplitoptions.removeemptyentries});//Remove empty Element (Result: "AAA" "BBSCC" "DD")

string []strarray = str. Split (new char[]{' t ', stringsplitoptions.none});//reserved empty element (Result: "AAA" "BBSCC" "dd" "")

4.string.split (string[],stringsplitoptions)

String str = "AAATBBSCCTDDT";

string []strarray = str. Split (New string[]{"T", stringsplitoptions.removeemptyentries});//Remove empty Element (Result: "AAA" "BBSCC" "DD")

string []strarray = str. Split (New string[]{"T", stringsplitoptions.none});//reserved empty element (Result: "AAA" "BBSCC" "dd" "")

5.string.split (char[],int32,stringsplitoptions)

String str = "AAATBBSCCTDDT";

string []strarray = str. Split (new char[]{' t ', 2,stringsplitoptions.removeemptyentries});//Cut into 2 parts and remove empty elements (Result: "AAA" "BBSCCTDDT")

string []strarray = str. Split (new char[]{' t ', 2,stringsplitoptions.none});//Cut into 2 parts and leave empty elements (Result: "AAA" "BBSCCTDDT" "")

6.string.split (string[],int32,stringsplitoptions)

String str = "AAATBBSCCTDDT";

string []strarray = str. Split (New string[]{"T", 2,stringsplitoptions.removeemptyentries});//Cut into 2 parts and remove empty elements (Result: "AAA" "BBSCCTDDT")

string []strarray = str. Split (New string[]{"T", 2,stringsplitoptions.none});//Cut into 2 parts and leave empty elements (Result: "AAA" "BBSCCTDDT" "")

Two, a regular match split string provides 5 overloaded functions:

name Description
Regex.Split (String) Splits the specified input string at the location defined by the regular expression pattern specified in the Regex constructor.

Supported by the. NET Compact Framework.

Regex.Split (String, Int32) Splits the specified input string by the specified maximum number of times in the position defined by the regular expression specified in the Regex constructor.

Supported by the. NET Compact Framework.

Regex.Split (String, String) Splits the input string at the location defined by the regular expression pattern.

Supported by the. NET Compact Framework.

Regex.Split (String, Int32, Int32) Splits the specified input string by the specified maximum number of times, starting at the specified character position in the input string, and at the location defined by the regular expression specified in the Regex constructor.

Supported by the. NET Compact Framework.

Regex.Split (String, String, RegexOptions) Splits the input string at the location defined by the specified regular expression pattern. You can specify options to modify the behavior of the match.

Supported by the. NET Compact Framework.

Using System.Text.RegularExpressions;

1.regex.split (string,string)

String str = "AAATSBBTSCCTSDD";
stirng []strarray = Regex.Split (str, "TS"); Regular matching cut (Result: "AAA" "BB" "CC" "DD");

2.regex.split (string,string)

String str = "AAATSBBTSCCTSDD";
stirng []strarray = Regex.Split (str, "TS", regexoptions.ignorecase); Regular matching cut (Result: "AAA" "BB" "CC" "DD");

Third, the application

1. Single character split

String str = "AAATBBSCCTDD";

string []strarray = str. Split (new char[]{' t '}); or string []strarray = str. Split (' t '); Single character cut (Result: "AAA" "BBSCC" "DD")

2. Multi-character splitting

String str = "AAATBBSCCTDD";

string []strarray = str. Split (new char[]{' t ', ' s '}); Multi-byte cut (Result: "AAA" "BB" "CC" "DD")

3. String splitting

Using System.Text.RegularExpressions;

String str = "AAATSBBTSCCTSDD";

stirng []strarray = Regex.Split (str, "TS", regexoptions.ignorecase); Regular matching cut and (Result: "AAA" "BB" "CC" "DD");

Or

String str = "AAATSBBTSCCTSDD";

string []strarray = str.  Split (New string[]{"TS"}); String cut (Result: "AAA" "BB" "CC" "DD")

Attention:

The 1.string.split method does not overload the String.Split (Stirng []) method, which means Str. Split ("T") or Str. Split (New string[]{"ts", "DD"}) are all wrong.

2. In addition to the String.Split (' t ') available for the cut single character, the other must be in a format such as String.Split (New char[]{}), or the compilation will be wrong

Split usage and explanations in C #

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.