Home > Article > Web Front-end > How to use indexOf() and the difference with jQuery.inArray()
The
indexOf() function is used to find the position where the sub string first appears in the current string. This function belongs to the String object and is supported by all major browsers.
Syntax
stringObject.indexOf( substring [, startIndex ] )
Parameters
substring String type subcharacter to be found string.
startIndex Optional/Number type The starting index to search in the current string, the default is 0.
Return value
The return value of the indexOf() method is of type Number, which returns the starting position (index) of the first search of the substring in the current string.
If not found, -1 is returned.
Example:
var str = "CodePlayer"; // 查找"Code",返回其第一次出现位置的起始索引 var index = str.indexOf("Code"); document.writeln(index); // 0 str = "jjkgdsgsabcgdhgdhj"; index = str.indexOf("j"); document.writeln(index); // 0 str = "abcsdjh230acabc"; // 从索引5处("j")开始查找"abc" index = str.indexOf("abc", 5); document.writeln(index); // 12 str = "abc"; index = str.indexOf("code"); // 找不到返回-1 document.writeln(index); // -1
indexOf() and lastIndexOf() are js usage and have nothing to do with jquery. Just use native js directly.
The indexOf() and lastIndexOf() methods return the position of the specified substring in another string. If the substring is not found, -1 is returned.
The difference between these two methods is that the indexOf() method starts to retrieve the string from the beginning of the string (position 0), while the lastIndexOf() method starts to retrieve the substring from the end of the string.
strObj.indexOf(subString[, startIndex]) Parameter strObj is required. String object or literal. subString is required. The substring to be found in the String object. starIndex optional. This integer value indicates the index within the String object at which to begin the search. If omitted, the search is from the beginning of the string.
Description 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: [CODE_HTML] [/CODE_HTML] The final result is -1,0,3
if(ss.indexOf('winner')>-1)
strobj.indexOf(substring[, startindex])
where strobj is a required option. string object or text
indexOf() and jQuery.inArray()
1.indexOf ()
When it is impossible to determine the location of a string When there is indeed a character in it, you can call the indexOf() and lastIndexOf() methods.
The indexOf() and lastIndexOf() methods return the position of the specified substring in another string. If the substring is not found, -1 is returned.
The difference between these two methods is that the indexOf() method starts to retrieve the string from the beginning of the string (position 0), while the lastIndexOf() method starts to retrieve the substring from the end of the string.
2.jquery.inArray()
jQuery.inArray(value, array) determines the position of the first parameter in the array, starting from 0 (if Returns -1 if not found). Return value: Number
For example:
var arr = [ "shtnl", "html", "js", "nodejs" ]; $.inArray("js", arr); //返回 2,
If it is not included in the array, it returns -1;
The above is the detailed content of How to use indexOf() and the difference with jQuery.inArray(). For more information, please follow other related articles on the PHP Chinese website!