根据汉字取首字母

Public Function getpychar(ByVal str As String) As String

tmp = 65536 + Asc(str)
If (tmp >= 45217 And tmp <= 45252) Then
getpychar = "A"
ElseIf (tmp >= 45253 And tmp <= 45760) Then
getpychar = "B"
ElseIf (tmp >= 45761 And tmp <= 46317) Then
getpychar = "C"
ElseIf (tmp >= 46318 And tmp <= 46825) Then
getpychar = "D"
ElseIf (tmp >= 46826 And tmp <= 47009) Then
getpychar = "E"
ElseIf (tmp >= 47010 And tmp <= 47296) Then
getpychar = "F"
ElseIf (tmp >= 47297 And tmp <= 47613) Then
getpychar = "G"
ElseIf (tmp >= 47614 And tmp <= 48118) Then
getpychar = "H"
ElseIf (tmp >= 48119 And tmp <= 49061) Then
getpychar = "J"
ElseIf (tmp >= 49062 And tmp <= 49323) Then
getpychar = "K"
ElseIf (tmp >= 49324 And tmp <= 49895) Then
getpychar = "L"
ElseIf (tmp >= 49896 And tmp <= 50370) Then '少了hen
getpychar = "M"
ElseIf (tmp >= 50371 And tmp <= 50613) Then
getpychar = "N"
ElseIf (tmp >= 50614 And tmp <= 50621) Then
getpychar = "O"
ElseIf (tmp >= 50622 And tmp <= 50905) Then
getpychar = "P"
ElseIf (tmp >= 50906 And tmp <= 51386) Then
getpychar = "Q"
ElseIf (tmp >= 51387 And tmp <= 51445) Then
getpychar = "R"
ElseIf (tmp >= 51446 And tmp <= 52217) Then '多余了一个
getpychar = "S"
ElseIf (tmp >= 52218 And tmp <= 52697) Then
getpychar = "T"
ElseIf (tmp >= 52698 And tmp <= 52979) Then
getpychar = "W"
ElseIf (tmp >= 52980 And tmp <= 53688) Then '范围有误
getpychar = "X"
ElseIf (tmp >= 53689 And tmp <= 54480) Then
getpychar = "Y"
ElseIf (tmp >= 54481 And tmp <= 62289) Then
getpychar = "Z"
Else '如果不是中文,则不处理
MsgBox "不是汉字"
End If

Function Getpy(str)
For i = 1 To Len(str)
Getpy = Getpy & getpychar(Mid(str, i, 1))
Next
End Function


Private Sub Command1_Click()
MsgBox Getpy("你好吗")
End Sub

注:参考文献:http://www.yywzw.com/jt/srh/hlg01-07.htm(Z 的右边界62289应该改为65278?)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
PHP可以使用以下代码来汉字首字母: ```php function getFirstLetter($str){ $result = ''; for ($i=0; $i < mb_strlen($str, 'utf8'); $i++) { $result .= getFirstChar(mb_substr($str, $i, 1, 'utf8')); } return $result; } function getFirstChar($str){ $fchar = ord(substr($str, 0, 1)); if ($fchar >= ord("A") and $fchar <= ord("Z") ) return $str; $asc = ord($str[0])*256 + ord($str[1])-65536; if ($asc >= -20319 and $asc <= -20284) return "A"; if ($asc >= -20283 and $asc <= -19776) return "B"; if ($asc >= -19775 and $asc <= -19219) return "C"; if ($asc >= -19218 and $asc <= -18711) return "D"; if ($asc >= -18710 and $asc <= -18527) return "E"; if ($asc >= -18526 and $asc <= -18240) return "F"; if ($asc >= -18239 and $asc <= -17923) return "G"; if ($asc >= -17922 and $asc <= -17418) return "H"; if ($asc >= -17417 and $asc <= -16475) return "J"; if ($asc >= -16474 and $asc <= -16213) return "K"; if ($asc >= -16212 and $asc <= -15641) return "L"; if ($asc >= -15640 and $asc <= -15166) return "M"; if ($asc >= -15165 and $asc <= -14923) return "N"; if ($asc >= -14922 and $asc <= -14915) return "O"; if ($asc >= -14914 and $asc <= -14631) return "P"; if ($asc >= -14630 and $asc <= -14150) return "Q"; if ($asc >= -14149 and $asc <= -14091) return "R"; if ($asc >= -14090 and $asc <= -13319) return "S"; if ($asc >= -13318 and $asc <= -12839) return "T"; if ($asc >= -12838 and $asc <= -12557) return "W"; if ($asc >= -12556 and $asc <= -11848) return "X"; if ($asc >= -11847 and $asc <= -11056) return "Y"; if ($asc >= -11055 and $asc <= -10247) return "Z"; return $str; } $hanzi = '你好'; echo getFirstLetter($hanzi); // 输出:NH ``` 以上代码是通过调用`getFirstLetter`函数来遍历输入的汉字字符串,然后调用`getFirstChar`函数来获每个汉字首字母。通过这样的方式,可以将汉字转换为拼音首字母
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值