VB 获取汉字拼音的首字母

这段VB代码提供了一个函数`py`,用于获取输入汉字的拼音首字母,通过ASCII码进行判断。另外,还有一个`test`函数用于将整个字符串转化为拼音首字母组合。在示例中,`test("中国人")`返回结果`ZGR`。
摘要由CSDN通过智能技术生成

Public Function py(mystr As String) As String
If Asc(mystr) < 0 Then
If Asc(Left$(mystr, 1)) < Asc("啊") Then
py = "0"
Exit Function
End If
If Asc(Left$(mystr, 1)) >= Asc("啊") And Asc(Left$(mystr, 1)) < Asc("芭") Then
py = "A"
Exit Function
End If
If Asc(Left$(mystr, 1)) >= Asc("芭") And Asc(Left$(mystr, 1)) < Asc("擦") Then
py = "B"
Exit Function
End If
If Asc(Left$(mystr, 1)) >= Asc("擦") And Asc(Left$(mystr, 1)) < Asc("搭") Then
py = "C"
Exit Function
End If
If Asc(Left$(mystr, 1)) >= Asc("搭") And Asc(Left$(mystr, 1)) < Asc("蛾") Then
py = "D"
Exit Function
End If
If Asc(Left$(mystr, 1)) >= Asc("蛾") And Asc(Left$(mystr, 1)) < Asc("发") Then
py = "E"
Exit Function
End If
If Asc(Left$(mystr, 1)) >= Asc("发") And Asc(Left$(mystr, 1)) < Asc("噶") Then
py = "F"
Exit Function
End If
If Asc(Left$(mystr, 1)) >= Asc("噶") And Asc(Left$(mystr, 1)) < Asc("哈") Then
py = "G"
Exit Function
End If
If Asc(Left$(mystr, 1)) >= Asc("哈") And Asc(Left$(mystr, 1)) < Asc("击") Then
py = "H"
Exit Function
End If
If Asc(Left$(mystr, 1)) >= Asc("击") And Asc(Left$(mystr, 1)) < Asc("喀") Then
py = "J"
Exit Function
End If
If Asc(Left$(mystr, 1)) >= Asc("喀") And Asc(Left$(mystr, 1)) < Asc("垃") Then
py = "K"
Exit Function
End If
If Asc(Left$(mystr, 1)) >= Asc("垃") And Asc(Left$(mystr, 1)) < Asc("妈") Then
py = "L"
Exit Function
End If
If Asc(Left$(mystr, 1)) >= Asc("妈") And Asc(Left$(mystr, 1)) < Asc("拿") Then
py = "M"
Exit Function
End If
If Asc(Left$(mystr, 1)) >= Asc("拿") And Asc(Left$(mystr, 1)) < Asc("哦") Then
py = "N"
Exit Function
End If
If Asc(Left$(mystr, 1)) >= Asc("哦") And Asc(Left$(mystr, 1)) < Asc("啪") Then
py = "O"
Exit Function
End If
If Asc(Left$(mystr, 1)) >= Asc("啪") And Asc(Left$(mystr, 1)) < Asc("期") Then
py = "P"
Exit Function
End If
If Asc(Left$(mystr, 1)) >= Asc("期") And Asc(Left$(mystr, 1)) < Asc("然") Then
py = "Q"
Exit Function
End If
If Asc(Left$(mystr, 1)) >= Asc("然") And Asc(Left$(mystr, 1)) < Asc("撒") Then
py = "R"
Exit Function
End If
If Asc(Left$(mystr, 1)) >= Asc("撒") And Asc(Left$(mystr, 1)) < Asc("塌") Then
py = "S"
Exit Function
End If
If Asc(Left$(mystr, 1)) >= Asc("塌") And Asc(Left$(mystr, 1)) < Asc("挖") Then
py = "T"
Exit Function
End If
If Asc(Left$(mystr, 1)) >= Asc("挖") And Asc(Left$(mystr, 1)) < Asc("昔") Then
py = "W"
Exit Function
End If
If Asc(Left$(mystr, 1)) >= Asc("昔") And Asc(Left$(mystr, 1)) < Asc("压") Then
py = "X"
Exit Function
End If
If Asc(Left$(mystr, 1)) >= Asc("压") And Asc(Left$(mystr, 1)) < Asc("匝") Then
py = "Y"
Exit Function
End If
If Asc(Left$(mystr, 1)) >= Asc("匝") Then
py = "Z"
Exit Function
End If
Else
If UCase$(mystr) <= "Z" And UCase$(mystr) >= "A" Then
py = UCase$(Left$(mystr, 1))
Else
py = mystr
End If
End If
End Function
Public Function test(str As String) As String
Dim tmp As String
For i = 1 To Len(str)
tmp = tmp & py(Mid$(str, i, 1))
Next i
test = tmp
End Function

Private Sub Form_Load()
MsgBox test("中国人") '结果: ZGR
End Sub

 
 

摘自:网络整理


VB相关


VB 读写TXT文本文件函数

VB 提取TextBox 文本框中指定一行字符串

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值