科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道汉字拼音的一个完整解决方法!

汉字拼音的一个完整解决方法!

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

   Imports System.IO Imports System.Text.RegularExpressions Namespace Businness.PinYin Public Class PYService Private gDataSe

作者:中国IT实验室 来源:中国IT实验室 2007年10月2日

关键字:

  • 评论
  • 分享微博
  • 分享邮件

在本页阅读全文(共2页)

  

For Each s As Char In line.ToCharArray
If IsTrue(s) Then
mBuilder.Append(GetPinyin(s, isgetfirst))
Else
mBuilder.Append(s)
End If
Next

Return mBuilder.ToString
End Function

Private Function GetPinyin(ByVal word As String, ByVal isgetfirst As Boolean) As String
Dim mResult As String = word

Dim mArray As String() = PinYinArray(ChineseCode(word)) '取拼音组
If Not mArray Is Nothing Then
If mArray.Length = 1 Or isgetfirst Then
mResult = mArray(0) '单音的
Else

mResult = String.Format("({0})", String.Join(",", mArray)) '多音的用括号括住,拼音间用逗号隔开
End If
End If

Return mResult
End Function

'取拼音组
Private Function PinYinArray(ByVal code As String) As String()
Dim mRow As dsPinYin.PinYinRow = Me.gDataSet.PinYin.FindBy代码(code)
If mRow Is Nothing Then Return Nothing

Return mRow.拼音.Split(" "c)
End Function

''' <summary>
''' 按拼音查字
''' </summary>
''' <param name="pinyin">拼音</param>
Public Function WordArray(ByVal pinyin As String) As String()
Dim mRows As dsPinYin.PinYinRow() = CType(Me.gDataSet.PinYin.Select(String.Format("拼音 LIKE '%{0}%'", pinyin)), dsPinYin.PinYinRow())
Dim mResult(-1) As String
For i As Integer = 0 To mRows.Length - 1
If Array.IndexOf(mRows(i).拼音.Split(" "c), pinyin) <> -1 Then
Me.Append(mResult, mRows(i).汉字)
End If
Next
Return mResult
End Function

''' <summary>
''' 按拼音查字
''' </summary>
''' <param name="pinyin">拼音</param>
Public Function Words(ByVal pinyin As String) As String
Return String.Concat(WordArray(pinyin))
End Function


''' <summary>
''' 汉字代码
''' </summary>
''' <param name="word">单个汉字</param>
Public Shared Function ChineseCode(ByVal word As String) As String
If Not IsTrue(word) Then Return Nothing

Dim bytes() As Byte = System.Text.Encoding.Default.GetBytes(word)

Return String.Concat(Hex(bytes(0)), Hex(bytes(1)))
End Function

''' <summary>
''' 是否是单个汉字
''' </summary>
''' <param name="word">字符</param>
Public Shared Function IsTrue(ByVal word As String) As Boolean
If word Is Nothing Then Return False

Return System.Text.RegularExpressions.Regex.IsMatch(word, "^[\u4e00-\u9fa5]$")
End Function

Private Sub Append(ByRef collection As String(), ByVal value As String)
ReDim Preserve collection(collection.Length)
collection(collection.Length - 1) = value
End Sub
End Class
End Namespace

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章