如何从typescript中的字符串中删除空格?

Tal*_*ode 17 trim typescript angular

在我的angular 5项目中,使用typescript我在这样的字符串上使用.trim()函数,但它不是删除空格而且也没有给出任何错误.

this.maintabinfo = this.inner_view_data.trim().toLowerCase();
// inner_view_data has this value = "Stone setting"
Run Code Online (Sandbox Code Playgroud)

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-1-4.html本文明确指出,这.trim()是打字稿的一部分.

从typescript中的字符串中删除空格的最佳方法是什么?

Hri*_*ale 32

trim()方法从字符串的两边删除空格.

你可以使用javascript替换方法来删除像

"hello world".replace(/\s/g, "");
Run Code Online (Sandbox Code Playgroud)

  • .Trim() 是删除开头或结尾的空格,而不是所有空格。 (2认同)

voi*_*oid 8

trim()方法从字符串的两边删除空格.

要删除字符串中的所有空格使用 .replace(/\s/g, "")

 this.maintabinfo = this.inner_view_data.replace(/\s/g, "").toLowerCase();
Run Code Online (Sandbox Code Playgroud)

  • 这将删除所有空格,尾随、前导以及中间的空格。 (2认同)

Jan*_*and 6

Trim 只是删除尾随和前导空格。如果只有空格要替换,请使用 .replace(//g, "")。

this.maintabinfo = this.inner_view_data.replace(/ /g, "").toLowerCase();
Run Code Online (Sandbox Code Playgroud)