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?
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.
Use JavaScript's string. replace() method with a regular expression to remove extra spaces. The dedicated RegEx to match any whitespace character is \s .
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.
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.
The trim() method removes whitespace from both sides of a string.
Source
You can use a Javascript replace method to remove white space like
"hello world".replace(/\s/g, "");
var out = "hello world".replace(/\s/g, ""); console.log(out);
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With