Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove whitespace from a string in typescript? [duplicate]

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?

like image 618
Talk is Cheap Show me Code Avatar asked Mar 07 '18 06:03

Talk is Cheap Show me Code


People also ask

How do I get rid of white spaces in a string in TypeScript?

Use the replace() method to remove all whitespace from a string in TypeScript, e.g. str. replace(/\s/g, '') . The replace method takes a regular expression and a replacement string as parameters. The method will return a new string with all whitespace removed.

How do I remove extra spaces from a string?

Use JavaScript's string. replace() method with a regular expression to remove extra spaces. The dedicated RegEx to match any whitespace character is \s .

How do you remove leading and trailing spaces in TypeScript?

trim() method is used to remove the white spaces from both the ends of the given string. Return value: This method returns a new string, without any of the leading or the trailing white spaces. In this example the trim() method removes all the leading and the trailing spaces in the string str.

How do you replace a space in TypeScript?

Use the String. replace() method to replace all spaces in a string, e.g. str. replace(/ /g, '+'); . The replace() method will return a new string with all spaces replaced by the provided replacement.


2 Answers

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);
like image 58
Hrishikesh Kale Avatar answered Sep 18 '22 07:09

Hrishikesh Kale


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(); 
like image 23
void Avatar answered Sep 20 '22 07:09

void