C # string. indexof Method

Source: Internet
Author: User
ArticleDirectory
    • Syntax
    • Description
    • Instance

Reprinted ......

String. indexof method (value, [startindex], [count])

The index of the first matching item of the specified character in this instance. Searches start from the specified character location and checks the specified number of character locations.

Parameters

Value

Unicode character to be searched. The search area of value is case sensitive.

Startindex (int32)

Optional. Start position of the search. If this parameter is not set, it starts from 0.

Count (int32)

Optional. number of characters to be checked.

Return Value

If this character is found, it is the index position of the value; otherwise, it is-1.

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:

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 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 where D appears 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 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:

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.

Syntax

Stringobject. indexof (searchvalue, fromindex)

Parameters

The description of searchvalue is required. Specifies the string value to be retrieved. Fromindex integer. Specifies the position where the search starts in the string. The valid value is 0 to stringobject. Length-1. If this parameter is omitted, It is retrieved from the first character of the string.
Description

This method retrieves the stringobject string from start to end to see if it contains the searchvalue. The start position is at the string's fromindex or the start of the string (when fromindex is not specified ). If a searchvalue is found, the first position of the searchvalue is returned. The character position in stringobject starts from 0.

Instance
In this example

)
)


" Hello world! " String for different searches:
< Script Type = " Text/JavaScript " > VaR Str = " Hello world! " Document. Write (Str. indexof ( " Hello " ) + " <Br/> " Document. Write (Str. indexof ( " World " ) + " <Br/> " Document. Write (Str. indexof ( " World " ))
</ Script > AboveCodeOutput:
0 - 1 6

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.