C # IndexOf Usage Deep Understanding _ Practical skills

Source: Internet
Author: User
IndexOf ()
Finds the first occurrence of a specified character or string in a string, and returns the first index value, such as:
Copy Code code as follows:

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

The indexof parameter is string, looking for the first occurrence of the parameter string in the string and returning the position. such as String s= "0123DFDFDF"; int i=s.indexof ("df"); then i==4.
If you need a more powerful string parsing feature, you should use the Regex class to match the string with a regular expression.

indexof (): To position characters and strings in a string from the previous back; all return values are in absolute position of the string, such as NULL 1
Copy Code code as follows:

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

LastIndexOf (): Locates characters and strings from behind in a string;
The usage is exactly the same as indexof ().
Here are IndexOfAny | | Lastindexofany
They accept the character array as the variable, and the other method, which returns the first subscript position of any character in the array.
As follows
Copy Code code 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 ditto.
====================================================================
substring () usage
String A= "AADSFDJKFGKLFDGLFD"
A.substring (5)//intercept all strings from the fifth digit
A.substring (0,5)//intercept all strings from No. 0 to 5th

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.