EN
TypeScript - trim whitespace from string
0
points
In this article, we would like to show you how to trim whitespace from a string in TypeScript.
Quick solution:
// ONLINE-RUNNER:browser;
const text: string = ' abc ';
const result: string = text.trim();
console.log(result);
Practical example
In this example, we use trim()
method that removes whitespace from both sides of a string.
const text1: string = ' abc';
const text2: string = 'abc ';
const text3: string = ' abc ';
const result1: string = text1.trim();
const result2: string = text2.trim();
const result3: string = text3.trim();
console.log(result1); // abc
console.log(result2); // abc
console.log(result3); // abc
Output:
abc
abc
abc