使用Extension Method計算漢字筆畫
public static int GetStrokesNumber(this char c)
{
String hex = BitConverter.ToString(Encoding.GetEncoding("Big5").GetBytes(new char[] { c })).Replace("-", string.Empty);
for (int i = 0; i < _strokesNumberData.Length; ++i)
{
for (int j = 0; j < _strokesNumberData[i].Length; j += 2)
{
if (hex.CompareTo(_strokesNumberData[i][j]) >= 0 && hex.CompareTo(_strokesNumberData[i][j + 1]) <= 0)
return i+1;
}
}
return 0;
}
}</pre></div>
Module CharExtension
Private _strokesNumberData As String()() = { _
New String() {"A440", "A441"}, _
New String() {"A442", "A453", "C940", "C944"}, _
New String() {"A454", "A47E", "C945", "C94C"}, _
New String() {"A4A1", "A4FD", "C94D", "C95C"}, _
New String() {"A4FE", "A5DF", "C95D", "C9AA"}, _
New String() {"A5E0", "A6E9", "C9AB", "C959"}, _
New String() {"A6EA", "A8C2", "CA5A", "CBB0"}, _
New String() {"A8C3", "AB44", "CBB1", "CDDC"}, _
New String() {"AB45", "ADBB", "CDDD", "D0C7", "F9DA", "F9DA"}, _
New String() {"ADBC", "B0AD", "D0C8", "D44A"}, _
New String() {"B0AE", "B3C2", "D44B", "D850"}, _
New String() {"B3C3", "B6C3", "D851", "DCB0", "F9DB", "F9DB"}, _
New String() {"B6C4", "B9AB", "DCB1", "E0EF", "F9D6", "F9D8"}, _
New String() {"B9AC", "BBF4", "E0F0", "E4E5"}, _
New String() {"BBF5", "BEA6", "E4E6", "E8F3", "F9DC", "F9DC"}, _
New String() {"BEA7", "C074", "E8F4", "ECB8", "F9D9", "F9D9"}, _
New String() {"C075", "C24E", "ECB9", "EFB6"}, _
New String() {"C24F", "C35E", "EFB7", "F1EA"}, _
New String() {"C35F", "C454", "F1EB", "F3FC"}, _
New String() {"C455", "C4D6", "F3FD", "F5BF"}, _
New String() {"C3D7", "C56A", "F5C0", "F6D5"}, _
New String() {"C56B", "C5C7", "F6D6", "F7CF"}, _
New String() {"C5C8", "C5C7", "F6D6", "F7CF"}, _
New String() {"C5F1", "C654", "F8A5", "F8ED"}, _
New String() {"C655", "C664", "F8E9", "F96A"}, _
New String() {"C665", "C66B", "F96B", "F9A1"}, _
New String() {"C66C", "C675", "F9A2", "F9B9"}, _
New String() {"C676", "C67A", "F9BA", "F9C5"}, _
New String() {"C67B", "C67E", "F9C6", "F9DC"}}
<Extension()> _
Public Function GetStrokesNumber(ByVal c As Char) As Integer
Dim hex As [String] = BitConverter.ToString(Encoding.GetEncoding("Big5").GetBytes(New Char() {c})).Replace("-", String.Empty)
For i As Integer = 0 To _strokesNumberData.Length - 1
For j As Integer = 0 To _strokesNumberData(i).Length - 1 Step 2
If hex.CompareTo(_strokesNumberData(i)(j)) >= 0 AndAlso hex.CompareTo(_strokesNumberData(i)(j + 1)) <= 0 Then
Return i + 1
End If
Next
Next
Return 0
End Function
End Module