如何获取nodejs中的字符串长度(以字节为单位)? [英] How to get the string length in bytes in nodejs?
本文介绍了如何获取nodejs中的字符串长度(以字节为单位)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何获取nodejs中的字符串长度(以字节为单位)?如果我有一个字符串,如下所示:äáöü
那么str.length将返回4.但是如何获得,字符串形成多少字节?
How to get the string length in bytes in nodejs? If I have a string, like this: äáöü
then str.length will return with 4. But how to get that, how many bytes form the string?
提前致谢
推荐答案
以下是一个例子:
str = 'äáöü';
console.log(str + ": " + str.length + " characters, " +
Buffer.byteLength(str, 'utf8') + " bytes");
// äáöü: 4 characters, 8 bytes
Buffer.byteLength(string,[encoding])
这篇关于如何获取nodejs中的字符串长度(以字节为单位)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文