Created
February 17, 2012 08:04
-
-
Save ishisaka/1851720 to your computer and use it in GitHub Desktop.
UNICODEの文字列がJISでのいわゆる半角文字相当で何文字か確認するプログラム。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
/* | |
* UNICODEの文字列がJISでのいわゆる半角文字相当で何文字か確認するプログラム。 | |
* | |
* 問題点: | |
* JIS以外の欧州文字(Latain-x)での1バイト文字とか全く気にしていない。 | |
*/ | |
namespace mojicounttest | |
{ | |
class Program | |
{ | |
//http://ash.jp/code/unitbl1.htm より文字コードをは引用した。 | |
static readonly char AsciiStart = ' '; | |
static readonly char AsciiEnd = '}'; | |
static readonly char AsciiTiruda = '~'; | |
static readonly char HalfKatakanaStart = '。'; | |
static readonly char HalfKatakanaEnd = '゚'; | |
static void Main(string[] args) { | |
string text = " 12345アイウエオ 12345あいうえお"; | |
TestChar(text); | |
text = "𠮟る";//𠮟がサロゲートペア文字 | |
TestChar(text); | |
TestCharWithSurrogatePair(text); | |
Console.Read(); | |
} | |
private static void TestChar(string text) { | |
int num = 0; | |
for (int i = 0; i < text.Length; i++) { | |
num += GetHanakuCharCounts(text[i]); | |
} | |
Console.WriteLine("元の文字列:{0}", text); | |
Console.WriteLine("文字数(半角相当): {0}", num); | |
} | |
private static void TestCharWithSurrogatePair(string text) { | |
int num = 0; | |
for (int i = 0; i < text.Length; i++) { | |
if (char.IsSurrogate(text[i])) { | |
num += 2; | |
i++; //次の文字(サロゲートペアのもう一文字)は読み飛ばす | |
continue; | |
} | |
num += GetHanakuCharCounts(text[i]); | |
} | |
Console.WriteLine("元の文字列:{0}", text); | |
Console.WriteLine("文字数(半角相当,サロゲートペア文字は全角1文字でカウント): {0}", num); | |
} | |
static int GetHanakuCharCounts(char c) { | |
if (c >= AsciiStart && c <= AsciiEnd) { | |
return 1; | |
} | |
else if (c == AsciiTiruda) { | |
return 1; | |
} | |
else if (c >= HalfKatakanaStart && c <= HalfKatakanaEnd) { | |
return 1; | |
} | |
else { | |
return 2; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment