当前位置:编程学习 > C#/ASP.NET >>

拼音简码解决方案

    前段时间在写药品管理程序,需要生成药品的拼音简码和通过拼音简码搜索药品。我觉得信息管理程序基本上都会用到。就分享给大家,其中的代码一部分是通过网络获得,遗憾的是没有记住分享的大大的名字,这里顺致敬意。
    Imports Microsoft.International.Converters.PinYinConverter
Public Class Classchineseconvert
    Public Shared Function GetPinYin(ByVal t1 As String) As String
        '取得输入行字的拼音码
        On Error Resume Next
        Dim sOne, sChar, sRet As String, i As Integer
        For i = 1 To Len(t1)
            sChar = Mid(t1, i, 1)
            sOne = ""
            If Asc(sChar) > 0 Then
                If UCase(sChar) <= "Z" And UCase(sChar) >= "A" Then
                    sOne = UCase(sChar)
                Else
                    sOne = ""
                End If
            Else : Dim chinesechar As New ChineseChar(sChar)
                sOne = chinesechar.Pinyins(0).Chars(0)
                'ElseIf Asc(sChar) >= Asc("啊") And Asc(sChar) < Asc("芭") Then
                '    sOne = "A"
                'ElseIf Asc(sChar) >= Asc("芭") And Asc(sChar) < Asc("擦") Then
                '    sOne = "B"
                'ElseIf Asc(sChar) >= Asc("擦") And Asc(sChar) < Asc("搭") Then
                '    sOne = "C"
                'ElseIf Asc(sChar) >= Asc("搭") And Asc(sChar) < Asc("蛾") Then
                '    sOne = "D"
                'ElseIf Asc(sChar) >= Asc("蛾") And Asc(sChar) < Asc("发") Then
                '    sOne = "E"
                'ElseIf Asc(sChar) >= Asc("发") And Asc(sChar) < Asc("噶") Then
                '    sOne = "F"
                'ElseIf Asc(sChar) >= Asc("噶") And Asc(sChar) < Asc("哈") Then
                '    sOne = "G"
                'ElseIf Asc(sChar) >= Asc("哈") And Asc(sChar) < Asc("击") Then
                '    sOne = "H"
                'ElseIf Asc(sChar) >= Asc("击") And Asc(sChar) < Asc("喀") Then
                '    sOne = "J"
                'ElseIf Asc(sChar) >= Asc("喀") And Asc(sChar) < Asc("垃") Then
                '    sOne = "K"
                'ElseIf Asc(sChar) >= Asc("垃") And Asc(sChar) < Asc("妈") Then
                '    sOne = "L"
                'ElseIf Asc(sChar) >= Asc("妈") And Asc(sChar) < Asc("拿") Then
                '    sOne = "M"
                'ElseIf Asc(sChar) >= Asc("拿") And Asc(sChar) < Asc("哦") Then
                '    sOne = "N"
                'ElseIf Asc(sChar) >= Asc("哦") And Asc(sChar) < Asc("啪") Then
                '    sOne = "O"
                'ElseIf Asc(sChar) >= Asc("啪") And Asc(sChar) < Asc("期") Then
                '    sOne = "P"
                'ElseIf Asc(sChar) >= Asc("期") And Asc(sChar) < Asc("然") Then
                '    sOne = "Q"
                'ElseIf Asc(sChar) >= Asc("然") And Asc(sChar) < Asc("撒") Then
                '    sOne = "R"
                'ElseIf Asc(sChar) >= Asc("撒") And Asc(sChar) < Asc("塌") Then
                '    sOne = "S"
                'ElseIf Asc(sChar) >= Asc("塌") And Asc(sChar) < Asc("挖") Then
                '    sOne = "T"
                'ElseIf Asc(sChar) >= Asc("挖") And Asc(sChar) < Asc("昔") Then
                '    sOne = "W"
                'ElseIf Asc(sChar) >= Asc("昔") And Asc(sChar) < Asc("压") Then
                '    sOne = "X"
                'ElseIf Asc(sChar) >= Asc("压") And Asc(sChar) < Asc("匝") Then
                '    sOne = "Y"
                'ElseIf Asc(sChar) >= Asc("匝") Then
                '    sOne = "Z"
            End If
            sRet = sRet & sOne
        Next i
        Return sRet
    End Function

End Class

 大家看到注释掉的部分是原来的解决方案,这种方案会有生字不 识别的缺陷。
于是我开头引用了微软的一个dll,可以在 http://download.microsoft.com/download/5/7/3/57345088-ACF8-4E9B-A9A7-EBA35452DEF2/vsintlpack1.zip
下载到。安装后引用 programfiles/internationalpack 下的chncharinfo.dll。
然后调用的话,非常简单,比如我把上面的class存为  Classchineseconvert.vb
直接调用  Classchineseconvert.GetPinYin(TextBox1.Text),在textbox1里输入你的汉字就会自动生成拼音简码。 --------------------编程问答-------------------- 貌似网上流传的这个有bug.忘了在哪里看到的了. --------------------编程问答-------------------- 感谢分享
补充:.NET技术 ,  VB.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,