In depth understanding of the usage of c indexof
- 2020-05-19 04:32:08
- OfStack
IndexOf()
Find the first occurrence of the specified character or string in the string, return the first index value, such as:
The indexof parameter is string, and you look in the string for the location of the first occurrence of the parameter string and return that location. Such as string s = "0123 dfdfdf"; int i = s. indexof (" df "); At this moment i = = 4.
If you need more powerful string parsing capabilities, you should use the Regex class, which USES regular expressions to match strings.
indexof() : position characters and strings from front to back in a string; All return values are at the absolute position of the string, or -1 if null
lastindexof() : position characters and strings from back to front in a string;
The usage is exactly the same as indexof().
IndexOfAny ||lastindexofany is introduced below
They accept an array of characters as arguments and, as above, return the first subscript of any character in the array
The following
lastindexofany ditto.
====================================================================
The usage substring ()
string a="aadsfdjkfgklfdglfd"
a.substring (5) // intercepts all strings after bit 5
a.substring (0,5) // intercepts all strings from 0 through 5
Find the first occurrence of the specified character or string in the string, return the first index value, such as:
str1.IndexOf(" word ") ; // Look for the word in str1 Index value (location) in
str1.IndexOf(" string ") ; // Look for the first part of the string 1 A character in str1 Index value (location) in
str1.IndexOf(" word ",start,end) ; // from str1 The first start+1 Characters up, search end Characters, find "word" in the string STR1 The position of [ From the first 1 It starts at 10 characters ] Pay attention to :start+end Is not greater than str1 The length of the
The indexof parameter is string, and you look in the string for the location of the first occurrence of the parameter string and return that location. Such as string s = "0123 dfdfdf"; int i = s. indexof (" df "); At this moment i = = 4.
If you need more powerful string parsing capabilities, you should use the Regex class, which USES regular expressions to match strings.
indexof() : position characters and strings from front to back in a string; All return values are at the absolute position of the string, or -1 if null
string test="asdfjsdfjgkfasdsfsgfhgjgfjgdddd";
test.indexof('d') =2 // Once upon a time back positioning d The first 1 The secondary position
test.indexof('d',5,2) =6 // Once upon a time back positioning d From the first 5 Bit start check, check 2 A, that is, From the first 5 Position to the first 7 A;
lastindexof() : position characters and strings from back to front in a string;
The usage is exactly the same as indexof().
IndexOfAny ||lastindexofany is introduced below
They accept an array of characters as arguments and, as above, return the first subscript of any character in the array
The following
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.
====================================================================
The usage substring ()
string a="aadsfdjkfgklfdglfd"
a.substring (5) // intercepts all strings after bit 5
a.substring (0,5) // intercepts all strings from 0 through 5