How to remove whitespace from a string in typescript?

AngularTypescriptTrim

Angular Problem Overview


In my angular 5 project, with typescript I am using the .trim() function on a string like this, But it is not removing the whitespace and also not giving any error.

this.maintabinfo = this.inner_view_data.trim().toLowerCase();
// inner_view_data has this value = "Stone setting"

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-1-4.html This docs clearly say that .trim() is part of the typescript.

What is best way to remove whitespace in from a string in typescript?

Angular Solutions


Solution 1 - Angular

#Problem

> The trim() method removes whitespace from both sides of a string.

Source

#Solution

You can use a Javascript replace method to remove white space like

"hello world".replace(/\s/g, "");

#Example

var out = "hello world".replace(/\s/g, "");
console.log(out);

Solution 2 - Angular

> The trim() method removes whitespace from both sides of a string.

To remove all the spaces from the string use .replace(/\s/g, "")

 this.maintabinfo = this.inner_view_data.replace(/\s/g, "").toLowerCase();

Solution 3 - Angular

Trim just removes the trailing and leading whitespace. Use .replace(/ /g, "") if there are just spaces to be replaced.

this.maintabinfo = this.inner_view_data.replace(/ /g, "").toLowerCase();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTalk is Cheap Show me CodeView Question on Stackoverflow
Solution 1 - AngularHrishikesh KaleView Answer on Stackoverflow
Solution 2 - AngularvoidView Answer on Stackoverflow
Solution 3 - AngularJan WendlandView Answer on Stackoverflow