How to calculate string length without spaces in TypeScript

1 Answer

0 votes
function lengthWithoutSpaces(str: string) {
    // Remove all spaces using a regular expression
    const stringWithoutSpaces: string = str.replace(/\s+/g, '');

    return stringWithoutSpaces.length;
}

const str: string = "TypeScript extends JavaScript by adding types to the language";

const len: number = lengthWithoutSpaces(str);

console.log(len); 


 
 
/*
run:

53
 
*/

 



answered Jan 11, 2025 by avibootz

Related questions

1 answer 106 views
1 answer 102 views
1 answer 110 views
1 answer 103 views
1 answer 85 views
1 answer 169 views
...