C # indexof usage

Source: Internet
Author: User

IndexOf ()
Searches for the position of the specified character or string that appears for the first time in the string, and returns the first index value, for example:
Copy codeThe Code is as follows:
Str1.IndexOf ("word"); // search for the index value (location) of the word in str1)
Str1.IndexOf ("string"); // search for the index value (location) of the first character of "string" in str1)
Str1.IndexOf ("Word", start, end); // query end characters starting from start + 1 in str1, search for the position of "word" in STR1 [starting from the first character] Note: start + end cannot exceed the length of str1

The indexof parameter is string. Search for the position where the parameter string appears for the first time in the string and return the position. For example, string s = "0123 dfdfdf"; int I = s. indexof ("df"); then I = 4.
If you need more powerful string parsing functions, use the Regex class and use regular expressions to match strings.

Indexof (): Locates characters and strings from the front and back of a string. All returned values are absolute positions of the string. If it is null, the return value is-1.
Copy codeThe Code is as follows:
String test = "asdfjsdfjgkfasdsfsgfhgjgfjgdddd ";
Test. indexof ('D') = 2 // locate the location where d appears for the first time.
Test. indexof ('D', 5, 2) = 6 // locate d from the front to the back and query it from 5th bits. Check for 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 is exactly the same as that of indexof.
The following describes IndexOfAny | lastindexofany.
They accept the character array as the variable element. The other methods are the same as above, and return the subscript location of any character in the array.
As follows:
Copy codeThe Code is 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 is the same as above.
========================================================== ======================================
Substring () Usage
String a = "aadsfdjkfgklfdglfd"
A. substring (5) // capture all strings after the fifth digit
A. substring (0th) // intercept all strings from 5th

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.