From cb9e359a51c3249d8f5157db69d43fd413ddeda6 Mon Sep 17 00:00:00 2001 From: Florian Merz Date: Thu, 11 Feb 2021 14:45:12 +0100 Subject: unslug ca: move --- .../global_objects/string/indexof/index.html | 190 +++++++++++++++++++++ 1 file changed, 190 insertions(+) create mode 100644 files/ca/web/javascript/reference/global_objects/string/indexof/index.html (limited to 'files/ca/web/javascript/reference/global_objects/string/indexof') diff --git a/files/ca/web/javascript/reference/global_objects/string/indexof/index.html b/files/ca/web/javascript/reference/global_objects/string/indexof/index.html new file mode 100644 index 0000000000..9b08b04ded --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/string/indexof/index.html @@ -0,0 +1,190 @@ +--- +title: String.prototype.indexOf() +slug: Web/JavaScript/Referencia/Objectes_globals/String/indexOf +translation_of: Web/JavaScript/Reference/Global_Objects/String/indexOf +--- +
{{JSRef}}
+ +

El mètode indexOf() retorna la primera posició dins el {{jsxref("String")}} des del que es crida a la qual es troba el valor proporcionat. Retorna -1 si no es troba el valor donat.

+ +

Sintaxi

+ +
str.indexOf(valorACercar[, posicioInicial])
+ +

Paràmetres

+ +
+
valorACercar
+
Un string que representa el valor a cercar.
+
posicioInicial {{optional_inline}}
+
La posició a partir de la qual es cercarà dins la cadena. Pot ser qualsevol nombre sencer. El valor per defecte és 0, indicant que es cercarà a tota la cadena. Si posicioInicial < 0 es cercarà a tota la cadena. Si posicioInicial >= str.length, no es cercarà a la cadena i es retornarà -1 automàticament. Si valorACercar és una cadena buida es retornarà str.length.
+
+ +

Descripció

+ +

Els caràcters de la cadena s'indexen d'esquerra a dreta. La posició del primer caràcter és 0, i la posició de l'últim caràcter d'una cadena amb nom stringName és  stringName.length - 1.

+ +
'Blue Whale'.indexOf('Blue');     // returns  0
+'Blue Whale'.indexOf('Blute');    // returns -1
+'Blue Whale'.indexOf('Whale', 0); // returns  5
+'Blue Whale'.indexOf('Whale', 5); // returns  5
+'Blue Whale'.indexOf('', 9);      // returns  9
+'Blue Whale'.indexOf('', 10);     // returns 10
+'Blue Whale'.indexOf('', 11);     // returns 10
+
+ +

Distinció entre majúscules i minúscules

+ +

El mètode indexOf() distingeix entre majúscules i minúscules. Per exemple, l'expressió següent retorna -1:

+ +
'Blue Whale'.indexOf('blue'); // retorna -1
+
+ +

Comprovar troballes

+ +

Cal destacar que '0' no s'evalua a true i que '-1' no s'evalua a false. Tenim llavors que al comprovar si una cadena específica existeix dins una altra, la forma correcta de comprovar-ho seria:

+ +
'Blue Whale'.indexOf('Blue') !== -1; // true
+'Blue Whale'.indexOf('Bloe') !== -1; // false
+
+ +

Exemples

+ +

Utilitzar indexOf() i lastIndexOf()

+ +

L'exemple següent utilitza indexOf() i {{jsxref("String.prototype.lastIndexOf()", "lastIndexOf()")}} per a trobar valors dins la cadena "Brave new world".

+ +
var anyString = 'Brave new world';
+
+console.log('La posicó de la primera w des del principi és ' + anyString.indexOf('w'));
+// mostra 8
+console.log('La posició de la primera w des del final és ' + anyString.lastIndexOf('w'));
+// mostra 10
+
+console.log('La posicó de "new" des del principi és ' + anyString.indexOf('new'));
+// mostra 6
+console.log('La posició de "new" des del final és ' + anyString.lastIndexOf('new'));
+// mostra 6
+
+ +

indexOf() i distinció entre majúscules i minúscules

+ +

L'exemple següent definteix dos variables de tipus cadena. The following example defines two string variables. The variables contain the same string except that the second string contains uppercase letters. The first {{domxref("console.log()")}} method displays 19. But because the indexOf() method is case sensitive, the string "cheddar" is not found in myCapString, so the second console.log() method displays -1.

+ +
var myString    = 'brie, pepper jack, cheddar';
+var myCapString = 'Brie, Pepper Jack, Cheddar';
+
+console.log('myString.indexOf("cheddar") is ' + myString.indexOf('cheddar'));
+// logs 19
+console.log('myCapString.indexOf("cheddar") is ' + myCapString.indexOf('cheddar'));
+// logs -1
+
+ +

Using indexOf() to count occurrences of a letter in a string

+ +

The following example sets count to the number of occurrences of the letter e in the string str:

+ +
var str = 'To be, or not to be, that is the question.';
+var count = 0;
+var pos = str.indexOf('e');
+
+while (pos !== -1) {
+  count++;
+  pos = str.indexOf('e', pos + 1);
+}
+
+console.log(count); // displays 4
+
+ +

Specifications

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES1')}}{{Spec2('ES1')}}Initial definition.
{{SpecName('ES5.1', '#sec-15.5.4.7', 'String.prototype.indexOf')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-string.prototype.indexof', 'String.prototype.indexOf')}}{{Spec2('ES6')}} 
{{SpecName('ESDraft', '#sec-string.prototype.indexof', 'String.prototype.indexOf')}}{{Spec2('ESDraft')}} 
+ +

Browser compatibility

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
+
+ +

See also

+ + -- cgit v1.2.3-54-g00ecf