C #,. net get the number, location, and replacement string of the specified string

Source: Internet
Author: User

Method 1:

Public static int indexOf (string/character, int starts from the nth digit, int queries several digits)

String tests = "1absjjkcbfka2rsbcfak2bfka1"; // test string

Int I = tests. IndexOf ("2b", 5, tests. Length-5); // search for "2b" from the fifth character"

TextBox2.Text = I. ToString ();

Int I = tests. IndexOf ("2b", 5, 2); // you can check whether two characters (2b) exist after the fifth character"

-1 indicates "2b" is not found"

 

LastIndexOf

Int g = tests. lastIndexOf ("2b", 5, tests. length-5); // indicates to search for "2b" from left to right of a sequential string, and return the position of the last "2b" in the string.

 

// Obtain the number of special strings in a string

String [] j = tests. Split ('A ');
TextBox1.Text = j. Length. ToString ();

Method 2:

TextBox2.Text = Regex. Matches (tests, "a"). Count. ToString (); // obtain the number of "a" in tests

 

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.