Search code examples
javascriptwhatsappstring-length

Why Whatsapp & Javascript return different length of same unicode string?


The unicode string in question:

ਪਹਿਲਾਂ ਲੋਕਾਂ ਦੇ ਦਿਲਾਂ ਦਿਮਾਗ਼ਾਂ ਚੋਂ ਇਹ ਵਹਿਮ ਕੱਢੋ ਕਿ 
ਅਸੀਂ ਹਿੰਦੂ ਹਾਂ,
ਅਸੀਂ ਸਿੱਖ ਹਾਂ,
ਅਸੀਂ ਮੁਸਲਮਾਨ ਹਾਂ,
ਅਸਲੀਅਤ ਇਹ ਹੈ ਕਿ 
ਅਸੀਂ ਭੁੱਖੇ ਹਾਂ, 
ਅਸੀਂ ਬੇਰੁਜ਼ਗਾਰ ਹਾਂ, 
ਅਸੀਂ ਨਸ਼ੇੜੀ ਹਾਂ, 
ਅਸੀਂ ਲਾਚਾਰ ਹਾਂ, 
ਅਸੀਂ ਬੇਵਕੂਫ਼ ਹਾਂ, 
ਅਸੀਂ ਬੀਮਾਰ ਹਾਂ, 

Language: Punjabi
Format: Unicode

Problem:
Javascript reports its str.length() = 226; whereas WhatsApp says 700 Characters.

Javascript Code:

console.log(inputStr.length);

Whatsapp Screenshot 01:
Fine, no error: (count at 698 or 699)

Whatsapp Screenshot 01

Just adding a new line from mobile keyboard makes it over 700 Whatsapp Screenshot 02

  1. Why there is a big mismatch in string length?
  2. Which number is real?
  3. How can I get the similar result in Javascript same as of Whatsapp result?

Solution

    1. This seems to be a bug with 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.

      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)

    2. The correct length is the javascript string length which is 226.

    Your text without newlines