Usage of substring () and split in C #

Source: Internet
Author: User

String. substring (INT index, int length)
Index: Start position, starting from 0
Length: the length of the substring you want to obtain

Example:
Using system;
Using system. Collections. Generic;
Using system. text;

Namespace str_sub
{
Class Program
{
Static void main (string [] ARGs)
{
String mystring = "Hello word! ";

// Substring () has two overload functions in C #
// Example

String substring1 = mystring. substring (0 );

// If the input parameter is an integer greater than or equal to 0,
// Start with the long position,
// All the remaining strings are taken after the truncation.
// If the input value is smaller than 0,
// The system throws an argumentoutofrange exception.
// Indicates that the parameter range is out of bounds.

String substring2 = mystring. substring (0, 5 );

// If two long integer parameters are input,
// The first one is the starting position of the original string.
// The last parameter is the length of the substring.
// The above exception also occurs if the conditions are not met

Console. writeline (substring1 );
Console. writeline (substring2 );
Console. Readline ();
}
}
}

Program output result:

Hello word!
Hello

 

// ++

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

 

// ++

Method 1:
String S = "abcdeabcdeabcde ";
String [] sarray = S. Split ('C ');
Foreach (string I in sarray)
Console. writeline (I. tostring ());
Console. readkey ();
Output the following results:
AB
Deab
Deab
De

Method 2:
We can see that the result is separated by a specified character. Use another constructor to separate multiple characters:
String S = "abcdeabcdeabcde ";
String [] sarray1 = S. Split (New char [3] {'C', 'D', 'E '});
Foreach (string I in sarray1)
Console. writeline (I. tostring ());
You can output the following results:
AB
AB
AB

Method 3:
In addition to the above two methods, the third method is to use a regular expression. Create a console project. Then add reference: using system. Text. regularexpressions;
String content = "agcyongfa365macyongfa365gggyongfa365ytx ";
String [] resultstring = RegEx. Split (content, "yongfa365", regexoptions. ignorecase );
Foreach (string I in resultstring)
Console. writeline (I. tostring ());
Console. readkey ();
Output the following results:
AGC
Mac
Ggg
Ytx

Method 4:
String str1 = "My ***** is ************************ Teacher ";
String [] str2;
Str1 = str1.replace ("*****","*");
Str2 = str1.split ('*');
Foreach (string I in str2)
Console. Write (I. tostring ());
Console. readkey ();

The most commonly used is str. Replace ("\ r \ n", "\ r"). Split ('\ R ')

Method 5:
String str1 = "I'm *** A ********************* Teacher ";
The result I want to display is: I am a teacher.
If I use the fourth method above, the following error will occur: I am a teacher. There is a space in the middle of the output, so the output result is not the expected result, which returns to the regular expression, then you can use the fifth method below:
String str1 = "I'm *** A ********************* Teacher ";
String [] str2 = system. Text. regularexpressions. RegEx. Split (str1, @ "\ * + ");
Foreach (string I in str2)
Console. Write (I. tostring ());
Console. readkey ();
Here, we achieved our goal through \ * +.

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.