Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove whitespace in text

How can I trim a text string in my Angular application?

Example

{{ someobject.name }}  

someobject.name results in "name abc"

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 | trim }} still results in "name abc" instead of "nameabc" }}

like image 555
Babulaas Avatar asked Mar 12 '18 13:03

Babulaas


Video Answer


1 Answers

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

If you want to remove all whitespaces use the replace function:

"name abc".replace(/\s/g, "");
like image 151
Jan B. Avatar answered Oct 12 '22 01:10

Jan B.