Created
February 27, 2018 15:34
-
-
Save L9m/04ba1d8bc8ad5f86709f902397fe36ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function countByte (str) { | |
return decodeURI( | |
encodeURI(str) | |
).length //URL-encode use UTF-8 | |
} //Note: this fails for input that contains lone surrogates. | |
} | |
Use http://mths.be/utf8js if you need something more robust. | |
function countByte (str) { | |
return str.length * 2 //JavaScript String use UTF-16 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment