如何删除文本中的空格 [英] How to remove whitespace in text
问题描述
如何在Angular应用程序中修剪文本字符串?
How can I trim a text string in my Angular application?
示例
{{ someobject.name }}
someobject.name结果为名称abc"
someobject.name results in "name abc"
我想要实现的是将名称命名为"nameabc" (删除所有空格).
What I like to achieve is name to be "nameabc" (remove all whitespaces).
我已经创建了一个管道,并将其包含在打字稿文件和模块中
I already created a pipe and included this in the typescript file and module)
PIPE:
import { Pipe, PipeTransform } from "@angular/core";
@Pipe({ name: 'trim' })
export class TrimPipe implements PipeTransform {
transform(value: any) {
if (!value) {
return '';
}
return value.trim();
}
}
{{someobject.name |修剪}}仍以"name abc"代替"nameabc"}}
{{ someobject.name | trim }} still results in "name abc" instead of "nameabc" }}
推荐答案
根据文档,trim()方法会删除 trailing 和 leading 空格,而不是中间.
According to the docs, the trim() method removes trailing and leading whitespaces, not those in the middle.
https://www.w3schools.com/Jsref/jsref_trim_string.asp
如果要删除所有空格,请使用replace
函数:
If you want to remove all whitespaces use the replace
function:
"name abc".replace(/\s/g, "");
这篇关于如何删除文本中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!