Home > Web Front-end > JS Tutorial > body text

How to use indexOf() and the difference with jQuery.inArray()

伊谢尔伦
Release: 2017-06-19 15:26:11
Original
5638 people have browsed it

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
Copy after login

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,
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template