半角は1文字、全角は2文字みたいに数えるやつ
ググるとshift_jisに変換してバイト数数えるやつがよく出てくるんですが、
Unity + Android環境だとI18N.dll突っ込まないと動かないらしいので正確じゃないけどそれっぽい動きするやつ。
正確にやりたい人は頑張ってください。
public static class StringUtil
{
/// <summary>
/// 英語は1文字=1、日本語は1文字=2みたいに数えるやつ
/// 正確ではない
/// </summary>
public static int Count(string text)
{
var one = BasicLatin.Concat(Latin1Supplement).Concat(LatinExtendedA).Concat(LatinExtendedB).Concat(LatinExtendedAdditional).ToArray();
return text.Sum(c =>
{
var cc = Convert.ToInt32(c);
return one.Any(x => x[0] <= cc && cc <= x[1]) ? 1 : 2;
});
}
private static readonly int[][] BasicLatin =
{
new[] { 0, 159 },
};
private static readonly int[][] Latin1Supplement =
{
new[] { 160, 255 },
};
private static readonly int[][] LatinExtendedA =
{
new[] { 256, 383 },
};
private static readonly int[][] LatinExtendedB =
{
new[] { 384, 591 },
};
private static readonly int[][] LatinExtendedAdditional =
{
new[] { 647, 669 },
};
}