Tutorialspoint.dev

JavaScript | String trim()

str.trim()
str.trim() function is used to remove the white spaces from both the ends of the given string. The syntax of the function is as follows:

str.trim()

Arguments
This function does not take any arguments.

Return value
This function returns a new string, without any of the leading or the trailing white spaces.

Examples for the above function are provided below:

Example 1:



var str = "GeeksforGeeks      ";
var st = str.trim();
print(st);

Output:

GeeksForGeeks

In this example the trim() function removes all the leading and the trailing spaces in the string str.

Example 2:

var str = "   GeeksforGeeks";
var st = str.trim();
print(st);

Output:

GeeksForGeeks

In this example the trim() function removes all the leading and the trailing spaces in the string str.

Codes for the above function are provided below:

Program 1:

<script>
// JavaScript Program to illustrate trim() function
  
function func() {
  
    // Original string containing whitespace
    var str = "GeeksforGeeks      ";
  
    // Trimmed string
    var st = str.trim();
    document.write(st); 
}
  
func();
</script>

Output:

GeeksForGeeks

Program 2:

<script>
// JavaScript Program to illustrate trim() function
  
function func() {
  
    // Original string containing whitespace
    var str = "   GeeksforGeeks";
  
    // Trimmed string
    var st = str.trim();
    document.write(st);  
}
func();
</script>

Output:



GeeksForGeeks

str.trimLeft()
str.trimLeft() function is used to remove the white spaces from the start of the given string. It does not affect the trailing white spaces. The syntax of the function is as follows:

str.trimLeft()

Arguments
This function does not take any arguments.

Return value
This function returns a new string, without any of the leading white spaces.

Examples for the above function are provided below:

Example 1:

var str = "      GeeksforGeeks      ";
var st = str.trimLeft();
print(st);

Output:

GeeksForGeeks

In this example the trimLeft() function removes all the leading spaces, but the trailing spaces in the string str remain as they are.

Example 2:

var str = "   GeeksforGeeks";
var st = str.trim();
print(st);

Output:

GeeksForGeeks

In this example the trimLeft() function removes all the leading spaces from str.



Codes for the above function are provided below:

Program 1:

<script>
// JavaScript Program to illustrate trimLeft()
// funtion
  
function func() {
  
    // Original string containing whitespace
    var str = "GeeksforGeeks      ";
  
    // Trimmed string
    var st = str.trimLeft();
    document.write(st);  
}
  
func();
</script>

Output:

GeeksForGeeks       

Program 2:

<script>
// JavaScript Program to illustrate trimLeft()
// function
  
function func() {
  
    // Original string containing whitespace
    var str = "   GeeksforGeeks";
  
    // Trimmed string
    var st = str.trimLeft();
    document.write(st);  
}
func();
</script>

Output:

GeeksForGeeks

str.trimRight()
str.trimRight() function is used to remove the white spaces from the end of the given string. It does not affect the white spaces at the start of the string.The syntax of the function is as follows:

str.trimRight()

Arguments
This function does not take any arguments.

Return value
This function returns a new string, without any of the trailing white spaces.

Examples for the above function are provided below:

Example 1:



var str = "GeeksforGeeks      ";
var st = str.trimRight();
print(st);

Output:

GeeksForGeeks

In this example the trimRight() function removes all the trailing spaces from the string str.

Example 2:

var str = "   GeeksforGeeks";
var st = str.trimRight();
print(st);

Output:

     GeeksForGeeks

In this example the trimRight() function does not remove the leading spaces from str.

Codes for the above function are provided below:

Program 1:

// JavaScript Program to illustrate trimRight()
// function
<script>
function func() {
  
    // Original string containing whitespace
    var str = "GeeksforGeeks      ";
  
    // Trimmed string
    var st = str.trimRight();
    document.write(st);  
}
func();
</script>

Output:

GeeksForGeeks       

Program 2:

<script>
// JavaScript Program to illustrate trimRight() 
// funtion
  
function func() {
  
    // Original string containing whitespace
    var str = "   GeeksforGeeks";
  
    // Trimmed string
    var st = str.trimRight();
    document.write(st);  
}
  
func();
</script>

Output:

     GeeksForGeeks


This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter