如何删除文本中的空格 [英] How to remove whitespace in text

查看:116
本文介绍了如何删除文本中的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆