aboutsummaryrefslogtreecommitdiff
path: root/files/it/web/javascript/reference/global_objects/string/includes/index.html
blob: 44eac8fc22b2d8dd7bc11948f58951ea9dd067a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
---
title: String.prototype.includes()
slug: Web/JavaScript/Reference/Global_Objects/String/includes
translation_of: Web/JavaScript/Reference/Global_Objects/String/includes
---
<div>{{JSRef}}</div>

<div>Il metodo <strong><code>includes() </code></strong>verifica se una stringa ne contiene un'altra desiderata, restituendo <code>true</code><code>false</code> in base dell'esito della ricerca.</div>

<div> </div>

<h2 id="Sintassi">Sintassi</h2>

<pre class="syntaxbox"><code><var>str</var>.includes(<var>searchString</var>[, <var>position</var>])</code></pre>

<h3 id="Parametri">Parametri</h3>

<dl>
 <dt><code>searchString</code></dt>
 <dd>Una stringa da cercare all'interno di una stringa.</dd>
 <dt><code>position</code></dt>
 <dd>Opzionale. La posizione in questa stringa. La posizione in questa stringa in cui iniziare la ricerca di searchString; il valore predefinito è 0.</dd>
</dl>

<h3 id="Valore_di_ritorno">Valore di ritorno</h3>

<p><strong><code>true</code></strong> se la stringa contiene la stringa di ricerca; altrimenti, <strong><code>false</code></strong>.</p>

<h2 id="Descrizione">Descrizione</h2>

<p>Questo metodo permette di determinare se la stringa includa o no un'altra stringa.</p>

<h3 id="Sensitività_alle_maiuscole">Sensitività alle maiuscole</h3>

<p>Il metodo <code>includes()</code> è sensibile alle maiuscole. Per esempio, la seguente espressione restituisce false:</p>

<pre class="brush: js">'Blue Whale'.includes('blue'); // returns false
</pre>

<h2 id="Esempi">Esempi</h2>

<h3 id="Utilizzando_includes()">Utilizzando <code>includes()</code></h3>

<pre class="brush: js">var str = 'To be, or not to be, that is the question.';

console.log(str.includes('To be'));       // true
console.log(str.includes('question'));    // true
console.log(str.includes('nonexistent')); // false
console.log(str.includes('To be', 1));    // false
console.log(str.includes('TO BE'));       // false
</pre>

<h2 id="Polyfill">Polyfill</h2>

<p>Questo metodo è stato aggiunto alla specifica ECMAScript 2015 e potrebbe essere non disponibile ancora in tutte le implementazioni di JavaScript.</p>

<pre class="brush: js">if (!String.prototype.includes) {
  String.prototype.includes = function(search, start) {
    'use strict';
    if (typeof start !== 'number') {
      start = 0;
    }

    if (start + search.length &gt; this.length) {
      return false;
    } else {
      return this.indexOf(search, start) !== -1;
    }
  };
}

/*
https://github.com/FabioVergani/js-Polyfill_StringIncludes/blob/master/StringIncludes.js

(function(s){'use strict';
 var o=s.prototype,p='includes';
 o[p]||(o[p]=function(a,b){//search,start
  var e=this,i=isNaN(b)?0:b,t=a,l=t.length;
  return (l&lt;1||((i+l)&gt;e.length))?false:-1!==e.indexOf(t,i);
 });
})(String);

*/</pre>

<p> </p>

<p> </p>

<p> </p>

<p> </p>

<h2 id="Specificazioni">Specificazioni</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-string.prototype.includes', 'String.prototype.includes')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Definizioni inizili.</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-string.prototype.includes', 'String.prototype.includes')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="Compatibilità_Browser">Compatibilità Browser </h2>

<div>{{CompatibilityTable}}</div>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Edge</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatChrome("41")}}</td>
   <td>{{CompatGeckoDesktop("40")}}</td>
   <td>{{CompatNo}}</td>
   <td>14393+</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatSafari("9")}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatGeckoMobile("40")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="String.prototype.contains">String.prototype.contains</h2>

<p>In Firefox 18 - 39, il nome di questo metodo era <code>contains()</code>. E' stato rinominato in<code>includes()</code> in {{bug(1102219)}} a causa del seguente motivo:</p>

<p>E' stato riportato che alcuni websites che utilizzano MooTools 1.2 non funzionavano su Firefox 17. Tale versione di MooTools controlla se <code>String.prototype.contains()</code> esiste e, se non esiste,  MooTools aggiunge una propria funzione. Con l'introduzione di questa funzione in Firefox 17, il comportamento di tale controllo è cambiato in un modo che il codice basato su <code>String.prototype.contains()</code>  non funzioni. Come risultato, l'implementazione  è stata disabilitata in Firefox 17 e <code>String.prototype.contains()</code> era disponibile nella versione successiva, in Firefox 18, quando <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=789036#c32">outreach to MooTools </a>stava conducendo al rilascio di <a href="http://mootools.net/blog/2013/02/19/mootools-1-2-6-released">MooTools version 1.2.6</a>.</p>

<p>MooTools 1.3 forza la propria versione di  <code>String.prototype.contains()</code>, così i siti web che si affidano ad essa non vanno in break. Comunque si noti che la signature di  <a href="http://mootools.net/core/docs/1.3.2/Types/String#String-method:-contains">MooTools 1.3 </a>e quella di ECMAScript 2015 per questo metodo differiscono (sul secondo argomento). Più avanti , <a href="https://github.com/mootools/mootools-core/blob/master/Docs/Types/String.md#note">MooTools 1.5+ ha cambiato la  signature per incontrare lo standard ES2015.</a></p>

<p>In Firefox 48, <code>String.prototype.contains()</code> è stato rimosso. Usare <code>String.prototype.includes()</code> solamente.</p>

<h2 id="Vedere_anche">Vedere anche</h2>

<ul>
 <li>{{jsxref("Array.prototype.includes()")}} {{experimental_inline}}</li>
 <li>{{jsxref("TypedArray.prototype.includes()")}} {{experimental_inline}}</li>
 <li>{{jsxref("String.prototype.indexOf()")}}</li>
 <li>{{jsxref("String.prototype.lastIndexOf()")}}</li>
 <li>{{jsxref("String.prototype.startsWith()")}}</li>
 <li>{{jsxref("String.prototype.endsWith()")}}</li>
</ul>