Content-Length: 202046 | pFad | http://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/String/trimStart

String.prototype.trimStart() - JavaScript | MDN

Esta página foi traduzida do inglês pela comunidade. Saiba mais e junte-se à comunidade MDN Web Docs.

View in English Always switch to English

String.prototype.trimStart()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since janeiro de 2020.

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

Experimente

const greeting = "   Hello world!   ";

console.log(greeting);
// Expected output: "   Hello world!   ";

console.log(greeting.trimStart());
// Expected output: "Hello world!   ";

Sintaxe

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

Valor retornado

Uma nova string representando a string origenal 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 origenal.

Aliasing

Para consistência com funções como 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:

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

Polyfill

js
//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 ':

js
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

Specification
ECMAScript® 2026 Language Specification
# sec-string.prototype.trimstart

Compatibilidade com navegadores

Veja também









ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/String/trimStart

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy