ソースコード
FullWidthToHalfWidthConverter.cs
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class FullWidthToHalfWidthConverter
{
private Encoding encoding = Encoding.GetEncoding("Shift_JIS");
private Dictionary<char, char> FullWidthToHalfWidthDictionary = new Dictionary<char, char>
{
{ 'ー','-'},
{ '「','['},
{ '」',']'},
{ '、',','},
{ '。','.'},
{ '・','/'},
};
private const int FullWidthHalfWidthDifference = 65248;
private const int StartFullWidthCharSetByte = 65281;
private const int EndFullWidthCharSetByte = 65374;
public string Convert(string text)
{
string output = "";
foreach (int c in text)
{
if (StartFullWidthCharSetByte <= c && c <= EndFullWidthCharSetByte) { output += (char)(c - FullWidthHalfWidthDifference ); continue; } if (FullWidthToHalfWidthDictionary.Keys.Any(k => (int)k == c))
{
output += FullWidthToHalfWidthDictionary[(char)c];
continue;
}
output += (char)c;
}
return output;
}
public bool IsHalfWidth(string text)
{
return text.Length == encoding.GetByteCount(text);
}
}
12345678910111213141516171819202122232425262728293031323334353637383940414243444546
FullWidthToHalfWidthConverter.csusing System.Collections.Generic;using System.Linq;using System.Text; public class FullWidthToHalfWidthConverter{ private Encoding encoding = Encoding.GetEncoding("Shift_JIS"); private Dictionary<char, char> FullWidthToHalfWidthDictionary = new Dictionary<char, char> { { 'ー','-'}, { '「','['}, { '」',']'}, { '、',','}, { '。','.'}, { '・','/'}, }; private const int FullWidthHalfWidthDifference = 65248; private const int StartFullWidthCharSetByte = 65281; private const int EndFullWidthCharSetByte = 65374; public string Convert(string text) { string output = ""; foreach (int c in text) { if (StartFullWidthCharSetByte <= c && c <= EndFullWidthCharSetByte) { output += (char)(c - FullWidthHalfWidthDifference ); continue; } if (FullWidthToHalfWidthDictionary.Keys.Any(k => (int)k == c)) { output += FullWidthToHalfWidthDictionary[(char)c]; continue; } output += (char)c; } return output; } public bool IsHalfWidth(string text) { return text.Length == encoding.GetByteCount(text); }}
説明 半角英数字は文 […]