diff options
Diffstat (limited to 'files/ca/web/javascript/reference/global_objects/string/trim/index.html')
-rw-r--r-- | files/ca/web/javascript/reference/global_objects/string/trim/index.html | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/files/ca/web/javascript/reference/global_objects/string/trim/index.html b/files/ca/web/javascript/reference/global_objects/string/trim/index.html new file mode 100644 index 0000000000..23bf4cdcf4 --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/string/trim/index.html @@ -0,0 +1,124 @@ +--- +title: String.prototype.trim() +slug: Web/JavaScript/Reference/Global_Objects/String/Trim +translation_of: Web/JavaScript/Reference/Global_Objects/String/Trim +original_slug: Web/JavaScript/Referencia/Objectes_globals/String/Trim +--- +<div>{{JSRef}}</div> + +<p>El mètode <strong><code>trim()</code></strong> elimina els espais en blanc tant a l'inici com al final del string. En aquest contexte, s'entèn com a espais en blanc tots les caràcters que no imprimeixin res (espai, tabulador, espai sense salt de línia, etcètera) així com tots els caràcters terminadors de línia (LF, CR, etc.).</p> + +<h2 id="Sintaxi">Sintaxi</h2> + +<pre class="syntaxbox"><code><var>str</var>.trim()</code></pre> + +<h2 id="Descripció">Descripció</h2> + +<p>El mètode <code>trim()</code> retorna un string sense espais en blanc tant a l'inici com al final. <code>trim()</code> no afecta el valor del string ja que en retorna un de nou.</p> + +<h2 id="Exemples">Exemples</h2> + +<h3 id="Utilitzar_trim()">Utilitzar <code>trim()</code></h3> + +<p>L'exemple següent mostra el string <code>'foo'</code>:</p> + +<pre class="brush: js">var orig = ' foo '; +console.log(orig.trim()); // 'foo' + +// Un altre exemple de .trim() eliminant espais en blanc només d'un cantó. + +var orig = 'foo '; +console.log(orig.trim()); // 'foo' +</pre> + +<h2 id="Polyfill">Polyfill</h2> + +<p>Executar el codi que es mostra a continuació abans d'executar cap altre codi crearà la funció <code>trim()</code> si aquesta no està disponible de forma nativa.</p> + +<pre class="brush: js">if (!String.prototype.trim) { + String.prototype.trim = function () { + return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, ''); + }; +} +</pre> + +<h2 id="Especificacions">Especificacions</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Especificació</th> + <th scope="col">Estat</th> + <th scope="col">Comentaris</th> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.5.4.20', 'String.prototype.trim')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td>Definició inicial. Implementat a JavaScript 1.8.1.</td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-string.prototype.trim', 'String.prototype.trim')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoDesktop("1.9.1")}}</td> + <td>{{CompatIE("9")}}</td> + <td>{{CompatOpera("10.5")}}</td> + <td>{{CompatSafari("5")}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</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>Suport bàsic</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Vegeu_també">Vegeu també</h2> + +<ul> + <li>{{jsxref("String.prototype.trimLeft()")}} {{non-standard_inline}}</li> + <li>{{jsxref("String.prototype.trimRight()")}} {{non-standard_inline}}</li> +</ul> |