Returns the character position for the first occurrence of a substring within a string object.
Public IndexOf (Value:string, [Startindex:number]): number
Searches for a string and returns the position of the first occurrence of value found on or after the StartIndex position within the call string. This index is zero-based, meaning that the first character in the string is considered to be at index 0 rather than index 1. If value is not found, the method returns-1.
Parameters
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 occurrence of the substring, or-1.
IndexOf Method
Returns the character position for the first occurrence of a substring within a string object.
Strobj.indexof (substring[, StartIndex])
Parameters
Strobj
Required option. A String object or text.
SubString
Required option. The substring to find in the string object.
Starindex
Options available. An integer value that indicates the index in which to start the lookup within a String object. If omitted, it is looked up from the beginning of the string.
Description
The IndexOf method returns an integer value that indicates the starting position of the substring of the string object. If no substring is found, returns-1.
If the startindex is a negative number, the startindex is treated as zero. If it is larger than the maximum character position index, it is considered to be the largest possible index.
Performs a lookup from left to right. Otherwise, the method is the same as LastIndexOf.
Example
The following example illustrates the use of the IndexOf method.
Copy Code code as follows:
function Indexdemo (str2) {
var str1 = "Babebibobubabebibobu"
var s = str1.indexof (STR2);
return (s);
}
Instance:
I get a string a for "1,18,33"
If it's written as a indexOf ("1") it doesn't seem to be able to find out what's more important is that 18 and 1 have a 1 in front, so the conditions are not accurate. How should I write?
IndexOf used this.
Copy Code code as follows:
String test = "1,18,33";
if (test. IndexOf ("1") >-1)
{
Response.Write ("existence");
}
Else
{
Response.Write ("does not exist");
}
But if only 1 meets the requirements, and 18 of the 1 does not meet the requirements, it cannot be done with indexof, so
Copy Code code as follows:
Using System.Text.RegularExpressions;
String test = "1,18,33";
if (Regex). IsMatch (Test, @ "\b1\b"))
{
Response.Write ("existence");
}
Else
{
Response.Write ("does not exist");
}
Comments:
\b Matches a word boundary in a regular
Wrote a method
Copy Code code as follows:
SRC Source string
Tar to compare strings
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;
}