How to Use the indexof and substring functions in C #

Source: Internet
Author: User

Indexof (): locates the character and string from the front to the back in the string. All return values are absolute positions in the string. If it is null, it is-1.

String test = "asdfjsdfjgkfasdsfsgfhgjgfjgdddd"; test. indexof (d) = 2 // locate the location where d appears for the first time

Test. indexof (d, 1) = 2 // locate the first position of d from the third string

Test. indexof (d, 5, 2) = 6 // locate d from the front to the back and query it from 5th bits. Check 2 bits, that is, from 5th bits to 7th bits;

Lastindexof (): locates the character and string from the back and forward in the string. The usage and indexof () are identical.
The following describes IndexOfAny | lastindexofany. They accept character arrays as variable elements. The other methods are the same as above. The earliest subscript position of any character in the array is returned as follows:

Char [] bbv = {s, c, B };

String abc = "acsdfgdfgchacstcsad ";

Response. Write (abc. IndexOfAny (bbv) = 1

Response. Write (abc. IndexOfAny (bbv, 5) = 9

Response. Write (abc. IndexOfAny (bbv, 5, 3) = 9 lastindexofany same as above

Substring () Usage

String a = "aadsfdjkfgklfdglfd" a. substring (5) // capture all strings after the fifth digit

A. substring (0th) // capture a string with a length of 5 starting from the position

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.