How to get the length of Japanese characters in Javascript?

后端 未结 2 1180
名媛妹妹
名媛妹妹 2021-01-13 01:05

I have an ASP Classic page with SHIFT_JIS charset. The meta tag under the page\'s head section is like this:



        
2条回答
  •  猫巷女王i
    2021-01-13 02:00

    You are getting confused between characters and bytes. 测 is ONE character, however you look at it. In UTF-16 (which is what Javascript uses), it's two BYTES. In Shift_JIS, 8 bytes, apparently. But in both cases, it's ONE character. So what you are trying to do is limit the text length to 200 BYTES. Since Javascript is using UTF-16 (UCS-2, really) you can get it's byte length by multiplying the string length by 2, but that doesn't help you with Shift_JIS. Then again, you should probably consider switching to Unicode anyway, if you're working with Javascript...

提交回复
热议问题