如何从打字稿中的字符串中删除空格?
How to remove whitespace from a string in typescript?
在我的 angular 5 项目中,我使用打字稿对这样的字符串使用 .trim() 函数,但它没有删除空格,也没有给出任何错误。
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 该文档清楚地表明 .trim()
是打字稿的一部分。
从打字稿中的字符串中删除空格的最佳方法是什么?
问题
The trim() method removes whitespace from both sides of a string.
解决方案
您可以使用 Javascript 替换方法来删除白色 space,例如
"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.
要删除字符串中的所有空格,请使用 .replace(/\s/g, "")
this.maintabinfo = this.inner_view_data.replace(/\s/g, "").toLowerCase();
Trim 只是删除尾随和前导空格。如果只有空格要替换,请使用 .replace(/ /g, "")。
this.maintabinfo = this.inner_view_data.replace(/ /g, "").toLowerCase();
在我的 angular 5 项目中,我使用打字稿对这样的字符串使用 .trim() 函数,但它没有删除空格,也没有给出任何错误。
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 该文档清楚地表明 .trim()
是打字稿的一部分。
从打字稿中的字符串中删除空格的最佳方法是什么?
问题
The trim() method removes whitespace from both sides of a string.
解决方案
您可以使用 Javascript 替换方法来删除白色 space,例如
"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.
要删除字符串中的所有空格,请使用 .replace(/\s/g, "")
this.maintabinfo = this.inner_view_data.replace(/\s/g, "").toLowerCase();
Trim 只是删除尾随和前导空格。如果只有空格要替换,请使用 .replace(/ /g, "")。
this.maintabinfo = this.inner_view_data.replace(/ /g, "").toLowerCase();