The IndexOf() method of a string searches whether the string passed as a parameter appears on the string. If the string is found, it returns the starting position of the character (0 means the first character, 1 means the second character follows By analogy) If it is not found, it returns -1. The IndexOf() method of the string searches whether the string passed as a parameter appears on the string. If the string is found, it returns the starting position of the character (0 means the first One character, 1 means the second character and so on) If it says not found, it returns -1
Returns the character position of the first occurrence of the substring within the String object.
Copy the code code as follows:
public indexOf(value:String, [startIndex:Number]) : Number
Searches a string and returns the position of the first occurrence of value found at or after startIndex within the calling string. This index is zero-based, which means that the first character in the string is considered to be at index 0 rather than index 1. If value is not found, this method returns -1.
parameter
value:String - a string; the substring to search for.
startIndex:Number [optional] - An integer specifying the starting index for the search.
return
Number - the position of the first occurrence of the specified substring, or -1.
-------------------------------------------------- -------------------------------------------------- -----------------------------------------------
indexOf method
Returns the character position of the first occurrence of the substring within the String object.
strObj.indexOf(subString[, startIndex])
parameter
StrOb
Required. String object or literal.
subString
Required. The substring to find in the String object.
starIndex
Optional. This integer value indicates the index within the String object to begin the search. If omitted, the search is from the beginning of the string.
illustrate
The indexOf method returns an integer value indicating the starting position of the substring within the String object. If the substring is not found, -1 is returned.
If startindex is negative, startindex is treated as zero. If it is greater than the largest character position index, it is treated as the largest possible index.
The search is performed from left to right. Otherwise, the method is the same as lastIndexOf.
Example
The following example illustrates the use of the indexOf method.
Copy the code code as follows:
function IndexDemo(str2){
var str1 = BABEBIBOBUBABEBIBOBU
var s = str1.indexOf(str2);
return(s);
}
Example:
I get a string a as 1,18,33
If it is written as a indexOf(1), it seems that it cannot be found. More importantly, there is a 1 in front of 18 and 1, so the conditions for establishment are not accurate. How should I write it?
indexOf is used like this
Copy the code code as follows:
string test = 1,18,33;
if (test.IndexOf(1) > -1)
{
Response.Write(exists);
}
else
{
Response.Write(does not exist);
}
But if only 1 meets the requirements, but 1 out of 18 does not meet the requirements, then IndexOf cannot be used, so
Copy the code code as follows:
using System.Text.RegularExpressions;
string test = 1,18,33;
if (Regex .IsMatch(test, @/b1/b))
{
Response.Write(exists);
}
else
{
Response.Write(does not exist);
}
Note:
/b matches a word boundary in the regex
Wrote a method
Copy the code code as follows:
//src source string
//tar string to be compared
private bool CheckString(string src, string tar)
{
string temp = Regex.Replace(tar, @[.$^{/[(|)*+?//], );
if (temp.Length < tar.Length)
return false;
if (Regex.IsMatch(src, @/b + tar + @/b))
return true;
return false;
}