Use of the string cut split function in asp.net

Source: Internet
Author: User

Syntax


Split (expression [, delimiter [, count [, start])


The syntax of the Split function includes the following parameters:


Expression is required. A string expression that contains a substring and a separator. If expression is a zero-length string, Split returns an empty array, that is, an array that does not contain elements and data.


Delimiter is optional. Character used to identify the substring boundary. If omitted, use space ("") as the separator. If delimiter is a zero-length string, a single element array containing the entire expression string is returned.


Count is optional. Number of substrings to be returned.-1 indicates that all substrings are returned.


Compare is optional. Indicates the value of the comparison type used to calculate the substring. The compare parameter can have the following values:
 

VbBinaryCompare 0 performs binary comparison.
VbTextCompare 1 performs text comparison.
 

The String. Split method has six overload functions:
 

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)

See the instance below


1. Introduce namespace

Using System. Text. RegularExpressions;

1. Separated by strings:

The code is as follows: Copy code

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:

The code is as follows: Copy code

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:

The code is as follows: Copy code

String str = "aaajbbbjccc ";
String [] sArray = str. Split ('J ');
Foreach (string I in sArray) Response. Write (I. ToString () + "<br> ");

Output result:
Aaa
Bbb
Ccc

Example 1: Use a single symbol to separate strings and use the split function to split strings.

Instance string: aaaa | ccccc | dddd | eeeee | ffff

The code is as follows:

The code is as follows: Copy code

String str = "aaaa | ccccc | dddd | eeeee | ffff ";
String [] array = str. Split ('| ');
Foreach (string I in array) Response. Write (I + "<br/> ");

Output result:

Aaaa
Ccccc
Dddd
Eeeee
Ffff

In the Split function, we pass in a char type, so we must use single quotation marks instead of writing them as follows: str. Split ("|") application overload:

 

Example 2: Use multiple characters to separate strings and use split groups to split strings.

Instance string: aaaa | ccccc, dddd, eeeee | ffff

The code is as follows:

The code is as follows: Copy code

String str = "aaaa | ccccc, dddd, eeeee | ffff ";
String [] array = str. Split (new char [2] {'| ',','});
Foreach (string I in array) Response. Write (I + "<br/> ");
Output result:

Aaaa
Ccccc
Dddd
Eeeee
Ffff

Because the required parameter in Split is a char array, we can input multiple char characters at a time. If our strings are separated by different symbols, then we can use this method to complete string splitting.

Example 3: Use regular matching to split strings.

Instance string: www.laozamao.comtiandaowww.baidu.comtiandaowww.lvyougang.com

We can see that this string is very strange, but in reality we may encounter this, that is, it is separated by a string, not a single character. For example, in this example, we use a string such as tiandao to separate them. At this time, we can hardly use the string split function, but it doesn't matter, we can use split in the regular expression to complete this function. The code is as follows:

First, reference the namespace: using System. Text. RegularExpressions;

The code is as follows: Copy code

String str = "www.111cn.nettiandaowww.baidu.comtiandaowww.111cn.net ";
String [] sArray = Regex. Split (str, "tiandao", RegexOptions. IgnoreCase );
Foreach (string I in sArray) Response. Write (I. ToString () + "<br/> ");
Output result:

Www.111cn.net
Www.baidu.com
Www.111cn.net

Extension Methods

In the above three examples, I have explained almost all the situations encountered in string separation, but I have omitted such a situation: Null element.

That is to say, after the separation, we may obtain a group of null data. Generally, we will determine whether it is null when we get the result, and then process it, in fact, the split function provides a solution for the null element, that is, the second parameter of split: StringSplitOptions, which is an enumeration type. Let's take a look at its value:

 

First: None indicates that the returned value includes an array element containing an empty string.

 

Second: RemoveEmptyEntries is the return value that does not include array elements containing null strings.

Now let's look at the Split function of the string and find that there are six times of overloading. The first parameter in these six reloads has two changes: char and string arrays, the second parameter is the number of returned results. For example, if there are five separated values, I only need the first three to limit the number of returned values in the second parameter, this will not be explained in detail. We will perform a small test on the two states of the third enumeration:

Example 4: The split function contains an empty element.

Instance string: aaaa, aaa

The code is as follows:

The code is as follows: Copy code

String str = "aaaa, aaa, aaa ";
String [] array = str. Split (new char [1] {','}, StringSplitOptions. None );
Foreach (string I in array) Response. Write (I + "<br/> ");
If the second parameter contains an empty element, the result is:

Aaaa

Aaa
Aaa
Aaa

 

Aaa

If we change the code:

The code is as follows: Copy code

String str = "aaaa, aaa, aaa ";
String [] array = str. Split (new char [1] {','}, StringSplitOptions. RemoveEmptyEntries );
Foreach (string I in array) Response. Write (I + "<br/> ");
Output result:

Aaaa
Aaa
Aaa
Aaa
Aaa

In this way, we do not need to add another string to determine whether it is a null string. Therefore, this is a very practical function.

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.