Languages
[Edit]
EN

TypeScript - trim whitespace from string

0 points
Created by:
a_horse
538

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
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join