PeeHaa PeeHaa - 4 months ago 7
Javascript Question

Best way to count characters in Javascript for a tweet

From the Twitter API docs ( http://dev.twitter.com/pages/counting_characters ):

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?

Answer

This is the correct answer: http://stackoverflow.com/a/10359930/240569

http://stackoverflow.com/a/10359930/240569

OUTDATED:

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