Skip to content

Instantly share code, notes, and snippets.

@sheepla
Last active April 20, 2024 07:16
Show Gist options
  • Save sheepla/b9d2b868c6407099825fb06512a0856f to your computer and use it in GitHub Desktop.
Save sheepla/b9d2b868c6407099825fb06512a0856f to your computer and use it in GitHub Desktop.
日本語の文字数をカウントする.ts
// https://zenn.dev/pandanoir/articles/how-to-count-strings
function countStringLength(text:string) : number {
const segmenter = new Intl.Segmenter("ja-JP", { granularity: "grapheme" })
const segments = [...segmenter.segment(text)]
console.log(segments)
return segments.length
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment