Typescript String trimEnd method
Last Updated :
14 May, 2024
The trimEnd
method, introduced in ECMAScript 2019 (ES10), is designed to remove whitespace characters from the end of a string. These whitespace characters include spaces, tabs, and line breaks.
Its primary purpose is to sanitize and normalize strings, especially user inputs or data fetched from external sources where leading or trailing whitespaces may be present unintentionally.
Syntax:
const trimmedString = originalString.trimEnd();
Parameters:
There are no parameters accepted by the trimEnd method.
Return Type:
After removing trailing whitespace characters, the procedure returns a new string.
Example 1: Implementation to use trimEnd method to eliminate trailing whitespace from a string.
JavaScript
let str: string = " Hello World ";
let trimmedStr: string = str.trimEnd();
console.log(trimmedStr);
Output:
Hello World
Example 2: Implementation to use trimEnd method to eliminate trailing whitespace from a string in conjunction with other string operations as well.
JavaScript
let userInput: string = " TypeScript is great! ";
let formattedInput: string =
userInput.trimEnd().toUpperCase();
console.log(formattedInput);
Output:
TYPESCRIPT IS GREAT!
Example 3: Implementation to use trimEnd method to eliminate trailing whitespace from a string and print its length for easy understanding.
JavaScript
let str = " GeeksforGeeks ";
console.log(str.length); // 19
str = str.trimEnd();
console.log(str.length); // 16
console.log(str); // ' GeeksforGeeks'
Output:
19
16
GeeksforGeeks
Example 4: Using trimEnd with Regular Expressions
JavaScript
let stringWithNumbers: string = "12345 ";
let trimmedNumbers: string = stringWithNumbers.replace(/\d+$/,'');
console.log(trimmedNumbers);
Output:
12345
Browser Support:
Share your thoughts in the comments
Please Login to comment...