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.

C# 文字数の調べ方

Posted at

##0.0 はじめに
stringなど文字列の長さ(文字数)の調べ方

##1.0 1文字づつ数える(半角、全角区別なし)

Lengthをつかう

Test.cs
string text1 = "リンゴ";
string text2 = "リンゴx5個"; // x5は半角
string text3 = "リンゴ x5個"; // x5は半角、スペースあり
int textCount = text1.Length; // textCount = 3
int textCount = text2.Length; // textCount = 6
int textCount = text3.Length; // textCount = 7

##2.0 全角と半角を分けて文字数を数える
全角は2、半角は1とするbyte数で文字数を数える方法です。
Shift_JISでByteにエンコードすることとなりますが、実機で動作させるため「I18N.CJK.dll」と「I18N.dll」をプロジェクトにインポートする必要があります。

Goro Azumaさんのページ、下記をご参照ください。

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?