How to Use asp.net string segmentation Functions

Source: Internet
Author: User

Let's look at a simple example.

But its array length is 25, rather than 3. The following method replaces "[jb51.net]" with a special character, for example, $.

For example, the following section is split according to [jb51.net ].

Copy codeThe Code is as follows:
String [] arrstr2 = str. Replace ("[jb51.net]", "$"). Split ('$ ');

Let's take a look at other methods, the simplest and most commonly used methods, and open vs.net to create a console project using a specified character. Enter the following program in the Main () method.

Copy codeThe Code is as follows:
String s = "abcdeabcdeabcde ";
String [] sArray = s. Split ('C ');
Foreach (string I in sArray)
Console. WriteLine (I. ToString ());

Output the following results:
Copy codeThe Code is as follows:
AB
Deab
Deab
De

2. Use multiple characters for segmentation

Copy codeThe Code is as follows:
String s = "abcdeabcdeabcde"
String [] sArray1 = s. Split (new char [3] {'C', 'D', 'E '});
Foreach (string I in sArray1)
Console. WriteLine (I. ToString ());

Output the following results:
Copy codeThe Code is as follows:
AB
AB
AB

3. Use Regular Expressions

Add reference
Copy codeThe Code is as follows:
Using System. Text. RegularExpressions;

String content = "agcsmallmacsmallgggsmallytx ";
String [] resultString = Regex. Split (content, "small", RegexOptions. IgnoreCase)
Foreach (string I in resultString)
Console. WriteLine (I. ToString ());

Output the following results:
Copy codeThe Code is as follows:
Agc
Mac
Ggg
Ytx

There is also an uncommon Method

Copy codeThe Code is as follows:
String str = "reterry [jb51.net] is the webmaster of [jb51.net ";
String [] arrstr = str. split (new char [] {'[','s ', 'O','s', 'U', 'O', '8 ','. ', 'C', 'O', 'M','] '});
For (int I = 0; I <arrstr. Length; I ++)
{
Response. Write (arrstr [I]);
}

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.