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 107 views
1 answer 103 views
1 answer 111 views
1 answer 104 views
1 answer 86 views
1 answer 170 views
...