English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript String indexOf() method

 Objet String JavaScript

indexOf()Returns the position of the first occurrence of the specified value in the string.

If the value is not found, it will return-1.

If the value exists multiple times, it will return the position of the first occurrence.

If you want to start searching from the beginning, uselastIndexOf()method.

Attention :For more information on Array methods, seeArray.indexOf().

Syntax :

string.indexOf(searchValue, start)
var str = 'Introduction de produits chimiques dans l'atmosphère';
str.indexOf('Pollution');// 4
Testez et voyez‹/›

Attention :Cette méthode distingue les majuscules et les minuscules.

Compatibilité du navigateur

Tous les navigateurs supportent complètement la méthode indexOf() :

Méthode
indexOf()EstEstEstEstEst

Valeur du paramètre

ParamètresDescription
searchValue(obligatoire) Représente la chaîne de caractères à rechercher
start(optionnel) Entier, représentant l'index de départ de la recherche ; par défaut 0

Détails techniques

Valeur de retour :la première apparitionsearchValuede l'index, s'il n'est pas trouvé, alors-1
Version JavaScript :ECMAScript 1

Plus d'exemples

Retournez la position du caractère 'L' dans la chaîne, à partir de la position6Commencez la recherche :

var str = 'HELLO WORLD HELLO';
str.indexOf('L', 6);
Testez et voyez‹/›

 Objet String JavaScript