| by Arround The Web | No comments

How to Left Trim and Right Trim String in JavaScript

Like other programming languages, in JavaScript, strings are an important form of variable, and the developers frequently need to edit or modify strings to fulfill their requirements, such as removing extra white spaces from a string, including “tab”, “space”, “line terminating characters” from either beginning or the end or on both sides of the string.

This article will explain the method of trimming the string from the right or the left side in JavaScript.

How to Left Trim and Right Trim String in JavaScript?

For left or right string trimming, JavaScript offers some built-in methods, including:

    • trim() method
    • trimLeft() method
    • trimRight() method

Let’s check out each of them one by one!

How to Use trim() Method in JavaScript?

The “trim()” method does not modify the original string, it only removes the whitespace characters from both sides, the starting, and end of a string.

Syntax

Follow the given syntax for using the trim() method to trim the strings:

string.trim();

 
The trim() method calls along the string that will be trimmed and returns a new string by eliminating extra white spaces from the specified string.

Example

First, we will create a “string” that contains extra white spaces at the start and the end of the string:

var string = "  Welcome to the LinuxHint       ";

 
Then, call the trim() method and store the resultant string in the variable “answer”:

var answer = string.trim();

 
Lastly, print the resultant string on the console using the “console.log()” method:

console.log(answer);

 
Now, we use the “length” property that returns the length of the string before and after the trimming:

console.log("Length of the Actual String is " + string.length);
console.log("Length of the Resultant String is " + answer.length);

 
As you can see in the output, the length of the actual string is “33” which contains spaces, and the resultant string’s length is “24”. This states that the white spaces from the start and the end of the string are successfully trimmed:


If you want to remove the whitespaces only from the start of the string, follow the below section.

How to Use trimLeft() Method in JavaScript?

The “trimLeft()” eliminates the leading white spaces in a string. It works similarly to the “trimStart()” method. Both methods act as the same because the trimStart() is the alias of the trimLeft() method.

Syntax

Use the following syntax for trimming the string from the left or the start of the string:

string.trimLeft();

 
Example

We will first create a string with three whitespaces at the beginning of the string and the same at the end of the string:

var string = "   Welcome to the LinuxHint   ";

 
Now, call the trimLeft() method to trim the spaces from the left side or start of the string:

var answer = string.trimLeft();

 
Finally, print the string on the console:

console.log(answer);

 
Check the length of the string before and after the trimming using the “length” property of the string:

console.log("Length of the Actual String is " + string.length);
console.log("Length of the Resultant String is " + answer.length);

 
The output shows that the “trimLeft()” method successfully trimmed the white spaces present at the beginning of the string:


You can also utilize the “trimStart()” method instead of the trimLeft() method for the same purpose:

var answer = string.trimStart();

 
It outputs the same result as the trimLeft() method:


Want to know the method to specifically remove extra spaces from the right side of the string? Follow the given-provided method.

How to Use trimRight() Method in JavaScript?

To trim the string from the right side of the string, use the JavaScript predefined “trimRight()” which is also known as the “trimEnd()” method. It is primarily utilized for removing spaces from the end of the string or from the string’s right side.

Syntax

The syntax for the trimRight() method is as follows:

string.trimRight();

 
Example

We will now utilize the same string and eliminate spaces from the right side of it by calling the “trimRight()” method:

var answer = string.trimRight();

 
It can be seen from the output that extra whitespaces from the end of the string are removed:


Now, use the “trimEnd()” method instead of the trimRight() method for the same scenario:

var answer = string.trimEnd();

 
Output


We have covered all the essential instructions related to the left and right trimming process of the string in JavaScript.

Conclusion

To the left and right trim string in JavaScript, use the JavaScript predefined methods including the “trim()” method, “trimLeft()” or “trimStart()” method, and the “trimRight()” or “trimEnd()” method. The trim() method trims the strings from the both left and the right of the string, the trimLeft() or the trimStart() method trims the string from the start, while the trimRight() or the trimEnd() method trims the string from the end. In this article, we have explained the procedure to trim the string from the right or the left side with detailed examples.

Share Button

Source: linuxhint.com

Leave a Reply