From 074785cea106179cb3305637055ab0a009ca74f2 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:42:52 -0500 Subject: initial commit --- .../global_objects/string/trimstart/index.html | 118 +++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 files/pt-br/web/javascript/reference/global_objects/string/trimstart/index.html (limited to 'files/pt-br/web/javascript/reference/global_objects/string/trimstart/index.html') diff --git a/files/pt-br/web/javascript/reference/global_objects/string/trimstart/index.html b/files/pt-br/web/javascript/reference/global_objects/string/trimstart/index.html new file mode 100644 index 0000000000..c784bc670a --- /dev/null +++ b/files/pt-br/web/javascript/reference/global_objects/string/trimstart/index.html @@ -0,0 +1,118 @@ +--- +title: String.prototype.trimStart() +slug: Web/JavaScript/Reference/Global_Objects/String/trimStart +tags: + - JavaScript + - Prototipo + - Referencia + - Remover espaços ao começo string + - String + - metodo + - trimStart() +translation_of: Web/JavaScript/Reference/Global_Objects/String/trimStart +--- +
{{JSRef}}
+ +

O método trimStart() remove espaços do começo de uma string. trimLeft() é um apelido para este método.

+ +
{{EmbedInteractiveExample("pages/js/string-trimstart.html")}}
+ + + +

Sintaxe

+ +
str.trimStart();
+str.trimLeft();
+ +

Valor retornado

+ +

Uma nova string representando a string original sem os espaços no começo (fim à esquerda).

+ +

Descrição

+ +

Os métodos trimStart() / trimLeft() retornam a string sem os espaços no fim à esquerda. trimLeft() ou trimStart() não altera o valor da string original.

+ +

Aliasing

+ +

Para consistência com funções como {{jsxref("String.prototype.padStart")}} o nome padrão do método é trimStart. Entretanto, por razões de compatibilidade na web, trimLeft permanece como um apelido para trimStart. Em alguns motores isso significa:

+ +
String.prototype.trimLeft.name === "trimStart";
+ +

Polyfill

+ +
//https://github.com/FabioVergani/js-Polyfill_String-trimStart
+
+(function(w){
+    var String=w.String, Proto=String.prototype;
+
+    (function(o,p){
+        if(p in o?o[p]?false:true:true){
+            var r=/^\s+/;
+            o[p]=o.trimLeft||function(){
+                return this.replace(r,'')
+            }
+        }
+    })(Proto,'trimStart');
+
+})(window);
+
+
+/*
+ES6:
+(w=>{
+    const String=w.String, Proto=String.prototype;
+
+    ((o,p)=>{
+        if(p in o?o[p]?false:true:true){
+            const r=/^\s+/;
+            o[p]=o.trimLeft||function(){
+                return this.replace(r,'')
+            }
+        }
+    })(Proto,'trimStart');
+
+})(window);
+*/
+ +

Exemplos

+ +

Usando trimStart()

+ +

O seguinte exemplo mostra uma string em caixa baixa 'foo  ':

+ +
var str = '   foo  ';
+
+console.log(str.length); // retorna 8
+
+str = str.trimStart();
+console.log(str.length); // retorna 5
+console.log(str);        // retorna 'foo  '
+
+ +

Especificações

+ + + + + + + + + + + + +
Especificação
{{SpecName('ESDraft', '#sec-string.prototype.trimstart', ' String.prototype.trimStart')}}
+ + + + + +

{{Compat("javascript.builtins.String.trimStart")}}

+ +

Veja também

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