C # several frequently used string interceptions,

Source: Internet
Author: User

C # several frequently used string interceptions,

Http://www.cnblogs.com/VicHuang/archive/2011/10/26/2225376.html

 

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 );


C language ^ how to use

A1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010

B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011

^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.

//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].

C language ^ how to use

A1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010

B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011

^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.

//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].

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.