C # string Truncation

Source: Internet
Author: User

String STR = "123abc456 ";
Int I = 3;
1. Take the first I character of the string.
STR = Str. substring (0, I); // or str = Str. Remove (I, str. Length-I );
2 remove the first I character of the string:
STR = Str. Remove (0, I); // or str = Str. substring (I );
3. Start from the right side with I characters:
STR = Str. substring (Str. Length-I); // or str = Str. Remove (0, str. Length-I );
4. Remove the I characters from the right:
STR = Str. substring (0, str. Length-I); // or str = Str. Remove (Str. Length-I, I );
5. If "ABC" exists in the string, remove it.
Using system. Text. regularexpressions;
String STR = "123abc456 ";
String A = "ABC ";
RegEx r = new RegEx ();
Match m = R. Match (STR );
If (M. Success)
{
// Take one of the two below.
STR = Str. Replace (,"");
Response. Write (STR );
String str1, str2;
Str1 = Str. substring (0, M. Index );
Str2 = Str. substring (M. index + A. length, str. Length-a.Length-m.Index );
Response. Write (str1 + str2 );
}
6. If the string contains "ABC", replace it with "ABC"
STR = Str. Replace ("ABC", "ABC ");

**************************************** ********

String STR = "adcdef"; int indexstart = Str. indexof ("D ");

Int endindex = Str. indexof ("e ");

String tostr = Str. substring (indexstart, endindex-indexstart );

C # The question of intercepting the last character of a string!

Str1.substring (str1.lastindexof (",") + 1 );

C # truncate the last character of the string

K = K. substring (K. Length-1, 1 );

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.