Horje
Typescript String trimEnd method

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:




Reffered: https://www.geeksforgeeks.org


JavaScript

Related
JavaScript Program for Lucas Numbers JavaScript Program for Lucas Numbers
TypeScript String replaceAll method TypeScript String replaceAll method
Find Number of Consonants in a String using JavaScript Find Number of Consonants in a String using JavaScript
JavaScript Program to Find First Set Bit JavaScript Program to Find First Set Bit
JavaScript Program to Check if a Number is Odd or Not using Bit Manipulation JavaScript Program to Check if a Number is Odd or Not using Bit Manipulation

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
14