Hi, everyone. Tell me, how to do .length not count spaces. Example, I want to write "1 2 3" that length was 3, not 5 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Hi, everyone. Tell me, how to do .length not count spaces. Example, I want to write "1 2 3" that length was 3, not 5

17th Apr 2021, 6:17 PM
Тимур Завьялов
Тимур Завьялов - avatar
4 Answers
+ 3
let str = "1 2 3 4 5" let res = str.split(" ") console.log(res.length)
17th Apr 2021, 6:29 PM
Avinesh
Avinesh - avatar
+ 3
console.log('1 2 3'.split(' ').length);
17th Apr 2021, 6:40 PM
Apongpoh Gilbert
Apongpoh Gilbert - avatar
+ 1
If you want to get rid of all whitespace, you can use this regular expression along with the replace method to replace whitespace with empty strings. "1 2 3".replace(/\s/g, "").length
17th Apr 2021, 6:23 PM
CamelBeatsSnake
CamelBeatsSnake - avatar