Below is the example of the String trim() Method. Show
JavaScript
GeeksForGeeks str.trim() method is used to remove the white spaces from both the ends of the given string. Syntax: str.trim() Return value: Examples for the above method are provided below: Example 1: var str = "GeeksforGeeks "; var st = str.trim(); print(st); Output: GeeksForGeeks In this example the trim() method 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() method removes all the leading and the trailing spaces in the string str. Codes for the above method are provided below: Program 1: JavaScript
Output: GeeksForGeeks Program 2: JavaScript
Output: GeeksForGeeks str.trimLeft() Syntax: str.trimLeft() Return value: Examples for the above method are provided below: Example 1: var str = " GeeksforGeeks "; var st = str.trimLeft(); print(st); Output: GeeksForGeeks In this example the trimLeft() method 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() method removes all the leading spaces from str. Codes for the above method are provided below: Program 1: JavaScript
Output: GeeksForGeeks Program 2: JavaScript
Output: GeeksForGeeks str.trimRight() Syntax: str.trimRight() Return value: Examples for the above method are provided below: Example 1: var str = "GeeksforGeeks "; var st = str.trimRight(); print(st); Output: GeeksForGeeks In this example the trimRight() method 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() method does not remove the leading spaces from str. Codes for the above method are provided below: Program 1: JavaScript
Output: GeeksForGeeks Program 2: JavaScript
Output: GeeksForGeeks Supported Browser:
What method can be used to remove any whitespace from both the beginning and the end of a string?The trim() method removes whitespace from both ends of a string and returns a new string, without modifying the original string.
Which method can be used to remove any whitespace from both the beginning and the end of a string in Python?Use the . strip() method to remove whitespace and characters from the beginning and the end of a string.
Which method can be used to remove any whitespace from both the beginning and the end of a string 1 point remove () trim () Strip () REM ()?String. Trim() removes all whitespace from the beginning and end of a string.
|