[英]Why Whatsapp & Javascript return different length of same unicode string?
The unicode string in question: 有问题的unicode字符串:
ਪਹਿਲਾਂ ਲੋਕਾਂ ਦੇ ਦਿਲਾਂ ਦਿਮਾਗ਼ਾਂ ਚੋਂ ਇਹ ਵਹਿਮ ਕੱਢੋ ਕਿ
ਅਸੀਂ ਹਿੰਦੂ ਹਾਂ,
ਅਸੀਂ ਸਿੱਖ ਹਾਂ,
ਅਸੀਂ ਮੁਸਲਮਾਨ ਹਾਂ,
ਅਸਲੀਅਤ ਇਹ ਹੈ ਕਿ
ਅਸੀਂ ਭੁੱਖੇ ਹਾਂ,
ਅਸੀਂ ਬੇਰੁਜ਼ਗਾਰ ਹਾਂ,
ਅਸੀਂ ਨਸ਼ੇੜੀ ਹਾਂ,
ਅਸੀਂ ਲਾਚਾਰ ਹਾਂ,
ਅਸੀਂ ਬੇਵਕੂਫ਼ ਹਾਂ,
ਅਸੀਂ ਬੀਮਾਰ ਹਾਂ,
Language: Punjabi 语言:旁遮普语
Format: Unicode 格式:Unicode
Problem: 问题:
Javascript reports its str.length() =
226 ; Javascript报告其
str.length() =
226 ; whereas WhatsApp says 700 Characters
. 而WhatsApp说
700 Characters
。
Javascript Code: Javascript代码:
console.log(inputStr.length);
Whatsapp Screenshot 01: Whatsapp截图01:
Fine, no error: (count at 698 or 699) 很好,没有错误:(数为698或699)
Just adding a new line from mobile keyboard makes it over 700 只需从移动键盘添加一条新线就可以超过700
This seems to be a bug with Whatsapp. 这似乎是Whatsapp的一个错误。 If you try inputting text with newlines, you will find that on the 15th line it will give the error of exceeding 700 characters, no matter what the current character count is.
如果您尝试使用换行符输入文本,您会发现在第15行,无论当前字符数是多少,它都会给出超过700个字符的错误。
If you try to put your string without the newline character it will work and not show the 700 character exceeded message (as shown in the image attached below) 如果您尝试将字符串设置为没有换行符,则它将起作用并且不显示超过700个字符的消息(如下面附带的图像所示)
The correct length is the javascript string length which is 226. 正确的长度是javascript字符串长度,即226。
That's probably because of the way 'length' is calculated. 这可能是因为计算了“长度”的方式。 Punjabi, like any other 'foreign' language, is based on the unicode.
与任何其他“外国”语言一样,旁遮普语基于unicode。 And unicode doesn't have a fixed length in which the character is stored.
并且unicode没有存储字符的固定长度。 The unicode character takes length anywhere from 1 byte to 4 bytes.
unicode字符的长度从1个字节到4个字节不等。 So, for example, the character 'ਕਿ' appears to be single but it may take 4 bytes.
因此,例如,字符“ਕਿ”似乎是单个字符,但可能需要4个字节。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.