[英]How to count real total number of chars in a string?
Sorry if the question is silly.对不起,如果这个问题很愚蠢。 I need to count real number of chars in a string.我需要计算字符串中的实际字符数。 For example I have the following string:例如我有以下字符串:
हा य
With .length
I have the following result:使用.length
我有以下结果:
let chars = "हा य"; console.log(chars.length); for(var i = 0; i < chars.length; i++){ console.log(chars[i]); }
As you can see, it's wrong.如您所见,这是错误的。 I have three chars here:我在这里有三个字符:
This one - हा
,这个 - हा
, - this one (empty space) and
य
this one. - 这个(空白)和य
这个。 Which is proper and shortest way to count it correctly?正确计算它的正确和最短方法是什么?
Hope this helps.希望这可以帮助。
For all languages you can try Count number of characters present in foreign language对于所有语言,您可以尝试计算外语中存在的字符数
function countChars(text){ return text.split("").filter( function(char){ let charCode = char.charCodeAt(); return charCode >= 2309 && charCode <=2361 || charCode == 32; }).length; } let chars = "हा य"; console.log(countChars(chars));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.