How to eliminate whitespace in a string?

2

How can I eliminate whitespace in a string? How can I put all the characters together and eliminate those spaces?

var incognita = "Hola como estas";
    
asked by user889 18.05.2018 в 18:08
source

2 answers

5

You can use String.replace () like this:

var incognita = "Hola como estas"
console.log(incognita.replace(/ /g, ""));
    
answered by 18.05.2018 в 18:12
1

If you want to replace from the html this is a way to do it. in this case I use a link HTML:

<a href="/dsoftware/{{item.id_proyecto}}/{{item.nombre | replacepipe}}">Enlace</a>

PIPE (create new file 'replace.pipe.ts'):

import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
  name: 'replacepipe'
})
export class ReplacePipe implements PipeTransform {

    transform(value: string): string {
        var re = / /gi; 
        var newstr = value.replace(re, "-"); 
        return newstr;
      }

}

MODULE:

import { ReplacePipe } from './replace.pipe';
...

declarations:[AppComponent, ... , ReplacePipe 
]
    
answered by 16.12.2018 в 04:56