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:
xxxxxxxxxx
1
// ONLINE-RUNNER:browser;
2
3
const text: string = ' abc ';
4
5
const result: string = text.trim();
6
7
console.log(result);
In this example, we use trim()
method that removes whitespace from both sides of a string.
xxxxxxxxxx
1
const text1: string = ' abc';
2
const text2: string = 'abc ';
3
const text3: string = ' abc ';
4
5
const result1: string = text1.trim();
6
const result2: string = text2.trim();
7
const result3: string = text3.trim();
8
9
console.log(result1); // abc
10
console.log(result2); // abc
11
console.log(result3); // abc
Output:
xxxxxxxxxx
1
abc
2
abc
3
abc