Search code examples
typescriptangular7angular8

How to remove all after space from string in angular 8


Tried to remove all after space and after underscore first letter should be caps from a string but not working.How do it in angular 8.If anyone know please help me to find a solution.

app.component.ts:

let content="power_managment 0vol";
alert(content.split( ).[0]);
// output should be like "powerManagment"

Solution

  • I think you want something like this:

    const capitalize = (s) => {
      if (typeof s !== 'string') return ''
      return s.charAt(0).toUpperCase() + s.slice(1)
    }
    
    let content="power_managment 0vol 0vol 0vol 0vol0vol 0vol test 123vol";
    let content2 = content.split(" ")[0].split("_");
    console.log(content2[0] + "" + capitalize(content2[1]))