How to remove the white space at the start of the string

I want to remove the white space which is there in the start of the string
It should remove only the space at the start of the string, other spaces should be there.

var string=' This is test';

This is what you want:

function ltrim(str) {
  if(!str) return str;
  return str.replace(/^\s+/g, '');
}

Also for ordinary trim in IE8+:

function trimStr(str) {
  if(!str) return str;
  return str.replace(/^\s+|\s+$/g, '');
}

And for trimming the right side:

function rtrim(str) {
  if(!str) return str;
  return str.replace(/\s+$/g, '');
}

Or as polyfill:

// for IE8
if (!String.prototype.trim)
{
    String.prototype.trim = function ()
    {
        // return this.replace(/^\s+|\s+$/g, '');
        return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
    };
}

if (!String.prototype.trimStart)
{
    String.prototype.trimStart = function ()
    {
        // return this.replace(/^\s+/g, '');
        return this.replace(/^[\s\uFEFF\xA0]+/g, '');
    };
}

if (!String.prototype.trimEnd)
{
    String.prototype.trimEnd = function ()
    {
        // return this.replace(/\s+$/g, '');
        return this.replace(/[\s\uFEFF\xA0]+$/g, '');
    };
}

Note:
\s: includes spaces, tabs \t, newlines \n and few other rare characters, such as \v, \f and \r.
\uFEFF: Unicode Character ‘ZERO WIDTH NO-BREAK SPACE’ (U+FEFF)
\xA0: ASCII 0xA0 (160: non-breaking space) is not recognised as a space character

Try to use javascript’s trim() function, Basically it will remove the leading and trailing spaces from a string.

var string=' This is test';
string = string.trim();

DEMO

So as per the conversation happened in the comment area, in order to attain the backward browser compatibility just use jquery’s $.trim(str)

var string=' This is test';    
string = $.trim(string)

You should use javascript trim function

var str = "       Hello World!        ";
alert(str.trim()); 

This function can also remove white spaces from the end of the string.

Easiest solution: (ES10 feature trimStart())

var string= 'This is a test';
console.log(string.trimStart());

.trimLeft() can be used for this.

const str = "   string   ";
console.log(str.trimLeft());     // => "string   "

Just a different and a not so efficient way to do it

var str = "   My string";

function trim() {
  var stringStarted = false;
  var newString = "";

  for (var i in str) {
    if (!stringStarted && str[i] == " ") {
      continue;
    }
    else if (!stringStarted) {
      stringStarted = true;
    }
    newString += str[i];
  }
  
  return newString;
}

console.log(trim(str));

I am really sure this doesn’t work for anything else and is not the most optimum solution, but this just popped into my mind

You want to remove the space (i.e whitespace) at the beginning of the string. So, could not use standard jquesy .trim(). You can use the regex to find and replace the whitespace at the beginning of the string.

Try this:

.replace(/^\s+/g, "")

Read this post

Try this:

var string='    This is test   ';
alert(string.replace(/^\s+/g, ""));

Working Example

OR if you want to remove the whitespace from the beginning and end then use .trim()

var string=' This is test';    
$.trim(string);


The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts