Given a String with unused Spaces, the task is to trim a string at the beginning, ending, or from both sides.
Below are the approaches to trim a string at the beginning or ending using JavaScript:
Table of Content
Trim a String at the Beginning with stimStart() Method
JavaScript trimStart() Method is used to remove white space from the beginning of a string. If white spaces is present after the string, it is not modified.
Syntax:
string.trimLeft();Example 1: In this example, a variable is declared with the string " GeeksforGeeks". Notice the given string that has whitespace at the left end. The trimStart() function will remove the whitespace at the beginning.
const str = " GeeksforGeeks";
// Trim the string at the Beginning
let newStr = str.trimStart();
console.log("Modified String: " + "'" + newStr + "'");
Output
Modified String: 'GeeksforGeeks'
Example 2: In this example, a variable is declared with the string " GeeksforGeeks ". Notice the given string that has whitespace at both ends. The trimStart() will only remove the whitespace at the beginning and leaves the whitespace at the end unchanged.
const str = " GeeksforGeeks ";
// Trimming the string at the start
let newStr = str.trimStart();
console.log("Modified String: " + "'" + newStr + "'");
Output
Modified String: 'GeeksforGeeks '
Trim a String at the End using trimEnd() Method
JavaScript trimEnd() Method is used to eliminate white-space from the end of a string. The string's value is not changed in any way, if any white space is present before the string, it's not modified.
Syntax:
string.trimEnd();Example 1: In this example, a variable is declared and string "GeeksforGeeks " is given to it. Notice the given string which has whitespace at the right end, so trimEnd() removes the whitespace at the end.
const str = "GeeksforGeeks ";
// Trimming the string at the right end
let newStr = str.trimRight();
console.log("Modified String: " + "'" + newStr + "'");
Output
Modified String: 'GeeksforGeeks'
Example 2: In this example, a variable is declared and string " GeeksforGeeks " is given to it. Notice the given string that has whitespace at both ends. The trimEnd() function removes the whitespace at the end and not at the beginning.
const str = " GeeksforGeeks ";
console.log("Initial String: " + "'" + word + "'");
// Trimming the string at the right end
let new_word = word.trimRight();
console.log("Modified String: " + "'" + new_word + "'");
Output
Initial String: ' geeksforgeeks ' Modified String: ' geeksforgeeks'
Trimming the string from both the ends
In this case, we trim the string at both ends using the trim() function.
JavaScript trim() Function: Trim() eliminates whitespace from both ends of a string and produces a new string with no changes to the original. All whitespace characters and all line terminator characters are considered whitespace in this context.
Syntax:
string.trim();Example: In this example, a variable var is declared and string " geeksforgeeks " is given to it. Notice the given string that has whitespace at both ends. The trim() removes the whitespace at both ends.
const word = " geeksforgeeks ";
console.log("Initial String: " + "'" + word + "'");
// Trimming the string at both ends
let new_word = word.trim();
console.log("Modified String: " + "'" + new_word + "'");
Output
Initial String: ' geeksforgeeks ' Modified String: 'geeksforgeeks'