Open In App

TypeScript String trim() Method

The trim() method is an inbuilt method of TypeScript. It is used to eliminate any kind of extra spaces, from the front and end to both sides of the provided string. It’s important to note that this function exists in JavaScript as well.

Syntax:

string.trim()

Parameters:

This method does not take any parameters.



Return Value:

It returns a new string that has been stripped of any leading or trailing whitespace.

Example 1: The below code example is a basic implementation of the trim() method in TypeScript.






const str: string = "   GeeksforGeeks   ";
const trimmedStr: string = str.trim();
console.log(trimmedStr);

Output:

GeeksforGeeks

Example 2: In below code the string is defined using the tab and other special space characters, we will remove it using the trim() method.




const str: string = "\tHello\n Geeks!\n";
const trimmedStr: string = str.trim();
console.log(trimmedStr);

Output:

Hello
Geeks!
Article Tags :