C # Index of usage (reproduced)

Source: Internet
Author: User

Finds the first occurrence of a specified character or string in a string, and returns the index value, such as:


Str1. IndexOf ("word");//Find the index value (position) of "word" in str1 str1. IndexOf ("string");//Find the index value (position) str1 of the first character of the string in str1. IndexOf ("word", start,end);//from str1 start+1 characters, look for end characters, find the position of "word" in string STR1 [from the first character] Note: Start+end cannot be greater than str1 length

The indexof parameter is string, looking for the position of the first occurrence of the argument string in the string and returning the position. such as String s= "0123DFDFDF"; int i=s.indexof ("df"), then i==4. Returns 1 if it is not found.

If you need more powerful string parsing, you should use a Regex class to match strings with regular expressions.

IndexOf (): Position characters and strings from the front to back in a string; all return values refer to the absolute position of the string, or 1 if null.


String test= "asdfjsdfjgkfasdsfsgfhgjgfjgdddd"; Test.indexof (' d ') =2          //front-to-back position D first occurrence test.indexof (' d ', 5,2) =6         //front-to-back positioning D starting from the 5th position, check 2, that is, from 5th to 7th;


LastIndexOf (): Positions characters and strings in a string from the back forward;
Usage is exactly the same as indexof ().

The following introduction IndexOfAny | | Lastindexofany

They accept the character array as a variable, as in the other way, returning any one of the characters in the array to the first occurrence of the subscript position

As follows:

Char[] bbv={' s ', ' C ', ' B '}; String abc = "Acsdfgdfgchacscdsad"; Response.Write (ABC. IndexOfAny (BBV)) =1 Response.Write (ABC. IndexOfAny (BBV, 5)) =9 Response.Write (ABC. IndexOfAny (BBV, 5, 3)) =9

Lastindexofany Ibid.

SUBSTRING () Usage

String A= "AADSFDJKFGKLFDGLFD"

A.substring (5)//intercept all strings after the fifth bit

A.substring (0,5)//intercept all strings from No. 0 to 5th

The above is the C # Index of the usage (reprint) content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.