PeeHaa PeeHaa - 3 months ago 5x
Javascript Question

Best way to count characters in Javascript for a tweet

From the Twitter API docs ( ):

the 140 chars tweet limit doesn't really count the characters but rather the bytes of the string.

How would I be able to count the bytes in a string using Javascript or does every character in my string always use 2 bytes since I set the encoding of my page to UTF-8?

Perhaps there is already a nice counter function for me to use?


This is the correct answer:


Why not just count the characters? For example from the twitter api

"Café" is either 5 or 6 UTF-8 bytes, but it is only supposed to be counted as 4 characters.

"café".length == 4

Just use String.length <= 140