How to Left Trim Strings in JavaScript

Waqar Aslam Feb 02, 2024
  1. Use regex With the replace() Function to Left Trim Strings in JavaScript
  2. Use the trimLeft() or trimStart() Function to Left Trim Strings in JavaScript
  3. Use the while Loop With the substring() Function to Left Trim Strings in JavaScript
How to Left Trim Strings in JavaScript

When we ask customers for information, we frequently deal with strings. Because of this, programmers are forced to work with many input strings that are sometimes inconsistent and may include whitespaces or other unusual characters.

In this article, we will go through how to eliminate the white spaces on the left side of a string. It may be accomplished in a variety of ways.

The replace() function, along with the regex tool, will be used in this article.

To begin, we will create an inconsistent test string by declaring a variable named name and assigning it a value that consists of five whitespaces placed before the beginning of the name, much like the code seen below.

let name = '     John Doe';

Every one of the sample programs will make use of the name variable.

Use regex With the replace() Function to Left Trim Strings in JavaScript

Now that we have access to the name variable, we can begin to work with it. To remove all of the whitespaces that are located before the first character, we will use the built-in replace() function on the string together with our regex and then store the resultant text in a result variable.

let result = name.replace(/^\s+/, '');

The replace() method requires two pieces of input from the user. The first parameter is the string or regex that needs to be replaced, and the second argument is the replacement requested for the first argument, which in our case is an empty string.

The first argument is the string or regex that needs to be replaced.

It is possible to describe the regex expression that is used with the replace() function as:

  • ^ stands for the beginning of a string
  • \s matches with whitespaces
  • + means one or more than one

The last thing we need to do is check that everything works as it should print out the output to our console. Below is the complete code for it.

let name = '    John Doe';
let result = name.replace(/^\s+/, '');
console.log(result);

Output:

John Doe

Use the trimLeft() or trimStart() Function to Left Trim Strings in JavaScript

In 2019, when ECMAScript 10 was made available, we were given a function named trimStart(). This method is part of the string class and removes the leading spaces from the string while trimming only the leading section of the string.

It is possible to do the call directly on the name variable.

let result = name.trimStart();

When we want the same outcomes, we can use a similar technique, trimLeft().

let result = name.trimLeft();

The complete code for trimLeft() is given below.

let name = '    John Doe';
let result = name.trimLeft();
console.log(result);

Output:

John Doe

Use the while Loop With the substring() Function to Left Trim Strings in JavaScript

To remove white spaces from the start of the text, we can use the while loop with the substring() function.

We will keep track of the number of white spaces by using a variable called index, and we will give it the value 0 when we define it.

let index = 0;

To some extent, strings are also considered to be objects in JavaScript. It indicates that the string is a collection of character indices.

A string may be looped over the same way an array can. We will iterate through our name string using the while loop, and for each whitespace, we will add a 1 to the index variable.

while (name[index] == ' ') {
  index++;
}

Now we have the name string and index as the number of the whitespaces before the name starts.

We can now call the substring() function and pass it the index as the count of whitespaces to get back the string without whitespaces and save that in a result variable.

let result = name.substring(index);

Finally, we will show the result on the console. The code for this method is given below.

let name = '    John Doe';
let index = 0;
while (name[index] == ' ') {
  index++;
}
let result = name.substring(index);
console.log(result);

Output:

John Doe

The functions explained here are compatible with all major browsers and JavaScript runtimes.

Author: Waqar Aslam
Waqar Aslam avatar Waqar Aslam avatar

I am Waqar having 5+ years of software engineering experience. I have been in the industry as a javascript web and mobile developer for 3 years working with multiple frameworks such as nodejs, react js, react native, Ionic, and angular js. After which I Switched to flutter mobile development. I have 2 years of experience building android and ios apps with flutter. For the backend, I have experience with rest APIs, Aws, and firebase. I have also written articles related to problem-solving and best practices in C, C++, Javascript, C#, and power shell.

LinkedIn

Related Article - JavaScript String