Метод trimStart()
видаляє пробіли з початку рядка. trimLeft()
є псевдонімом цього методу.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Синтаксис
str.trimStart(); str.trimLeft();
Значення, що вертається
Новий рядок, який відображає початковий рядок без пробілів на початку (з лівого кінця).
Опис
Методи trimStart()
/ trimLeft()
повертають рядок з прибраними пробілами з лівого кінця. trimLeft()
чи trimStart()
не змінюють значення самого рядка.
Псевдонім
Для сумісності з такими функціями, як String.prototype.padStart
, стандартним ім'ям методу є trimStart
. Однак, з причин веб-сумісності trimLeft
залишається в якості псевдоніму trimStart
. В деяких рушіях це означає:
String.prototype.trimLeft.name === "trimStart";
Приклади
Використання trimStart()
Наступний приклад виводить рядок з малих літер 'ква '
:
var str = ' ква ';
console.log(str.length); // 8
str = str.trimStart();
console.log(str.length); // 5
console.log(str); // 'ква '
Специфікації
Специфікація |
---|
ECMAScript (ECMA-262) The definition of ' String.prototype.trimStart' in that specification. |
Сумісність з веб-переглядачами
BCD tables only load in the browser
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Поліфіл
//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);
*/