I have ported this method to vb.net, please see source code (II)

Source: Internet
Author: User
Tags exit chr count end integer net string tostring
#Region "from Pinyin to Pinyin"
Public Shared Function getpytopy (ByVal pystr as String, _
Optional ByVal Iscomp as Boolean = False) as String
If pystr.length = 0 Then Exit Function

miscompelled = Iscomp

Dim Tonesite as Integer
Dim Tonevalue as Integer
Dim Tonechar as Char

Tonesite = Gettonesite (PYSTR)
If tonesite =-1 Then
Return PYSTR
ElseIf Tonesite <-1 Then
Exit Function
End If

Tonevalue = Gettonevalue (Pystr, Tonesite)
If Tonevalue =-1 Then Exit Function

If miscompelled Then
Tonechar = Gettonechar (Pystr, toneSite-1)
Else
Tonechar = Gettonechar (PYSTR)
End If
If not Ispychar (Tonechar) Then Exit Function
return conversion (PYSTR, Tonesite, Tonevalue, Tonechar)

End Function
' Returns a position indicating the number of tones
Private Shared Function gettonesite (ByVal pystr as String) as Integer
If pystr.length = 0 Then Exit Function
Dim I As Integer, j As Integer = 0
' Check that there are several numbers in the string
For i = 0 to Pystr.length-1
If Char.isnumber (Pystr.chars (i)) Then
J + 1
End If
Next
If j = 0 Then ' No digits, that is, no value indicating the tone
Return-1
ElseIf j = 1 Then ' has a number, legal
For i = 0 to Pystr.length-1
If Char.isnumber (Pystr.chars (i)) Then
return I
End If
Next
Else ' There are multiple numbers, illegal, return the opposite number
Return-j
End If
End Function
' Return tone is a few voices
Private Shared Function gettonevalue (ByVal pystr as String, _
ByVal Site As Integer) As Integer
If pystr.length = 0 Then Exit Function
If Site >= 0 Then
Dim value as Char = Pystr.chars (Site)
Dim Num as Integer
If Char.isnumber (value) Then
num = CType (value. ToString, Integer)
If num >= 0 and num <= 4 Then
Return num
Else
Return-1
End If
Else
Return-1
End If
End If
End Function
' Return the letter to mark the tone
Private Shared Function Gettonechar (ByVal pystr as String, _
ByVal Site as Integer) as Char
If pystr.length = 0 Then Exit Function
If Site >= 0 Then
Dim value as Char = Pystr.chars (Site)
return value
End If
End Function
Private Shared Function Gettonechar (ByVal pystr as String) as Char
If pystr.length = 0 Then Exit Function
Dim Chr as Char

If Pystr.indexof (A (0)) >= 0 Then
CHR = Pystr.chars (Pystr.indexof (A (0)))
ElseIf pystr.indexof (o (0)) >= 0 Then
CHR = Pystr.chars (Pystr.indexof (o (0)))
ElseIf Pystr.indexof (E (0)) >= 0 Then
CHR = Pystr.chars (Pystr.indexof (E (0)))
ElseIf Pystr.indexof (i (0)) >= 0 Then
CHR = Pystr.chars (Pystr.indexof (i (0)))
ElseIf pystr.indexof (U (0)) >= 0 Then
CHR = Pystr.chars (pystr.indexof (U (0)))
ElseIf Pystr.indexof (V (0)) >= 0 Then
CHR = Pystr.chars (Pystr.indexof (V (0)))
End If

Return CHR
End Function
' Judge whether the phonetic characters are valid
Private Shared Function Ispychar (ByVal chr as Char) as Boolean
Select Case CHR
Case "a" C
Return True
Case "O" C
Return True
Case "E" C
Return True
Case "I" C
Return True
Case "U" c
Return True
Case "V" c
Return True
Case Else
Return False
End Select
End Function
' Convert
Private Shared Function conversion (ByVal Pystr as String, _
ByVal Tonesite as Integer, _
ByVal Tonevalue as Integer, _
ByVal Tonechar as Char) as String
If tonevalue >= 1 and tonevalue <= 4 Then
Select Case Tonechar
Case "a"
Pystr = Pystr.replace (Tonechar, A (tonevalue). Chars (0))
Case "O"
Pystr = Pystr.replace (Tonechar, O (tonevalue). Chars (0))
Case "E"
Pystr = Pystr.replace (Tonechar, E (tonevalue). Chars (0))
Case "I"
Pystr = Pystr.replace (Tonechar, I (Tonevalue). Chars (0))
Case "U"
Pystr = Pystr.replace (Tonechar, U (tonevalue). Chars (0))
Case "V"
Pystr = Pystr.replace (Tonechar, V (tonevalue). Chars (0))
End Select
Pystr = Pystr.replace (tonevalue.tostring, "")
Return PYSTR
End If
End Function
#End Region

#Region "from Chinese to Pinyin"
Public Shared Function gethztopy (ByVal hzstr as String, _
Optional ByVal tonevalue as Integer = 0, _
Optional ByVal Iscomp as Boolean = False) as String
' ByVal Tonevalue as Integer, _
If hzstr.length = 0 Then Exit Function
If pylist.count = 0 Then loadpychars ()
miscompelled = Iscomp
If miscompelled Then
Else
Dim tmpstr as String = ""
Dim I as Integer
Dim Num as Integer
For i = 1 to Hzstr.length
num = ASC (HZSTR)
Debug.WriteLine (Num. ToString)
TMPSTR = Getpychars (num)
Tmpstr = Getpytopy (Tmpstr & Tonevalue)
Return TMPSTR
Next
End If
End Function

Private Shared Function getpychars (ByVal num as Integer) as String
If num > 0 and num < 160 Then
return Chr (num)
Else
If num < -20319 Or num > -10247 Then
Return ""
Else
Dim I, value as Integer
For i = pylist.count-1 to 0 Step-1
If Pylist.getbyindex (i) <= num Then Exit for
Next
' Value = CType (Pylist.getbyindex (i), Integer)
return Pylist.getkey (i). Tostring
End If
End If
End Function
#End Region

End Class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.