Asp.net string segmentation function usage

Source: Internet
Author: User
Tags foreach array length tostring

Let's look at a simple example.

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

For example, if I want to split it according to [111cn.net],

The code is as follows: Copy code

String [] arrstr2 = str. Replace ("[111cn.net]", "$"). Split ('$ ');

Check other methods.


, The simplest and most commonly used method. Open vs.net to create a console project by separating a specified character. Enter the following program in the Main () method.

The code is as follows: Copy code

String s = "abcdeabcdeabcde ";
String [] sArray = s. Split ('C ');
Foreach (string I in sArray)
Console. WriteLine (I. ToString ());

Output the following results:
AB
Deab
Deab
De

2. Use multiple characters for segmentation

The code is as follows: Copy code

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:
AB
AB
AB

3. Use regular expressions

Add reference

The code is as follows: Copy code

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:
Agc
Mac
Ggg
Ytx


There is also an uncommon method


The code is as follows:

The code is as follows: Copy code
String str = "reterry [111cn.net] is the webmaster of [111cn.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]);
}

 

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.