LoginSignup
2

More than 3 years have passed since last update.

サロゲートペア、異体字、絵文字に対応した文字数の数え方

Posted at

spread operatorだけでサロゲートペア文字列は対応できる

"𠮷野屋".length //=> 4
[..."𠮷野屋"].length // => 3

異体字や絵文字対応するなら、ライブラリを使う

import split from "graphemesplit";

"𠮷野屋👍🏾葛飾区👍葛󠄀城市👍🏿".length // => 22
[..."𠮷野屋👍🏾葛飾区👍葛󠄀城市👍🏿"].length // => 15
split("𠮷野屋👍🏾葛飾区👍葛󠄀城市👍🏿").length // => 12

使うライブラリ

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
2