简体   繁体   English

如何轻松检查字符串中的空格数?

[英]How to easily check the number of whitespaces in a string?

Basically for every letter in a string I will be increasing an integer by 1. The string length function would work perfectly, but it will include spaces as part of the length. 基本上对于字符串中的每个字母,我将增加1的整数。字符串长度函数将完美地工作,但它将包括空格作为长度的一部分。 I only want the number of characters in a string, not including spaces. 我只想要一个字符串中的字符数,不包括空格。

How can you check how many spaces a string has? 如何检查字符串有多少个空格? I could then subtract that value from the string.length . 然后我可以从string.length减去该值。

这用于计算字符串中非空字符的数量:

yourString.replace(/\s/g, "").length

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM