SoFunction
Updated on 2025-03-10

Method introduction

The IndexOf() method of the string searches whether a string passed as a parameter appears on the string. If a string is found, it returns the starting position of the character (0 means the first character, 1 means the second character and so on). If it is not found, it returns -1
Returns the character position of the first substring in the String object.
Copy the codeThe code is as follows:

public indexOf(value:String, [startIndex:Number]) : Number

Searches for the string and returns the position of the first match of the value found on or after the startIndex position inside the call string. This index starts at zero, which means that the first character in the string is considered to be at index 0 rather than index 1. If no value is found, the method returns -1.
parameter
value:String - A string; a substring to search for.
startIndex:Number [optional] - An integer that specifies the start index of the search.
return
Number - Specifies the position of the first match of the substring, or -1.
--------------------------------------------------------------------------------------------------------------------------------------------------
indexOf method
Returns the character position of the first substring in the String object.
(subString[, startIndex])
parameter
strObj
Required option. String object or text.
subString
Required option. The substring to look for in the String object.
starIndex
Optional. This integer value indicates the index that begins searching within the String object. If omitted, look up from the beginning of the string.
illustrate
The indexOf method returns an integer value indicating the start position of the substring inside the String object. If no substring is found, return -1.
If startindex is a negative number, startindex is treated as zero. If it is larger than the largest character position index, it is regarded as the largest possible index.
Perform a lookup from left to right. Otherwise, the method is the same as lastIndexOf.
Example
The following example illustrates the usage of the indexOf method.
Copy the codeThe code is as follows:

function IndexDemo(str2){
var str1 = "BABEBIBOBUBABEBIBOBU"
var s = (str2);
return(s);
}


Example:
I get a string a is "1,18,33"
If 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 validity are inaccurate. How should I write it?

Use indexOf in this way
Copy the codeThe code is as follows:

string test = "1,18,33";
if (("1") > -1)
{
("exist");
}
else
{
("Not exists");
}

But if only 1 meets the requirements and 1 of 18 does not meet the requirements, then you cannot use IndexOf to do it.
Copy the codeThe code is as follows:

using ;
string test = "1,18,33";
if (Regex .IsMatch(test, @"\b1\b"))
{
("exist");
}
else
{
("Not exists");
}

Notes:
\b Match a word boundary in the regular
Write a method
Copy the codeThe code is as follows:

//src Source string
//tar To be compared string
private bool CheckString(string src, string tar)
{
string temp = (tar, @"[.$^{\[(|)*+?\\]", "");
if ( < )
return false;
if ((src, @"\b" + tar + @"\b"))
return true;
return false;
}