0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Salesforce】半角カナ文字の文字数を調べてみた

Last updated at Posted at 2021-03-31

Stringクラスの「length」メソッドを使ってカナ文字の文字数を調べてみました。
全角と半角で文字数に違いはあるのでしょうか?

全角カナ文字の文字数は?

まず、全角カナ文字の文字数を確認してみます。
濁点ありでも試してみましたが、いずれの文字数も1文字でした。

// 全角「カ」の文字数は: 1
System.debug('全角の文字数は: ' + 'カ'.length());

// 全角「ガ」の文字数は: 1
System.debug('全角の文字数は: ' + 'ガ'.length());

半角カナ文字の文字数は?

続いて、半角カナ文字の文字数を確認してみます。
全角カナ文字と結果は異なり、濁点ありの文字数は2文字でした。

// 半角「カ」の文字数は: 1
System.debug('半角の文字数は: ' + 'カ'.length());

// 半角「ガ」の文字数は: 2
System.debug('半角ガの文字数は: ' + 'ガ'.length());

まとめ

全角カナ文字は期待値通り、文字数は1文字となりました。
しかし、半角カナ文字については濁点の有無で文字数が変化するため注意が必要そうです。

参考

■String クラス

0
0
0

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
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?