如何获得汉字的首字母? create function f_ch2py(@chn nchar(1)) returns char(1) as begin declare @n int declare @c char(1) set @n = 63select @n = @n +1, @c = case chn when @chn then char(@n) else @c end from( select top 27 * from ( select chn = '吖' union all select '八' union all select '嚓' union all select '咑' union all select '妸' union all select '发' union all select '旮' union all select '铪' union all select '丌' union all select --because have no 'i' '丌' union all select '咔' union all select '垃' union all select '嘸' union all select '拏' union all select '噢' union all select '妑' union all select '七' union all select '呥' union all select '仨' union all select '他' union all select '屲' union all select --no 'u' '屲' union all select --no 'v' '屲' union all select '夕' union all select '丫' union all select '帀' union all select @chn) as a order by chn COLLATE Chinese_PRC_CI_AS ) as b return(@c) endgo select dbo.f_ch2py('斌') --B select dbo.f_ch2py('国') --G select dbo.f_ch2py('人') --R select dbo.f_ch2py('镆') --M drop function f_ch2py
上面的是针对数据库的,下面的不需要数据库:方法1: Private Sub Command1_Click() MsgBox pinyin("不到黄河心不死!") End Sub Function pinyin(ByVal x As String) As String On Error Resume Next Const hanzi = "啊芭擦搭蛾发噶哈击喀垃妈拿哦啪期然撒塌挖昔压匝座ABCDEFGHJKLMNOPQRSTWXYZZ" pinyin = "" Dim temp As String, i As Long, j As Long i = 1 Do While i <= Len(x) If Mid(x, i, 1) = "座" Then temp = "Z" If InStr(",。“”;:?》《!·¥()", Mid(x, i, 1)) > 0 Then temp = "" For j = 1 To 23 If Asc(Mid(x, i, 1)) >= Asc(Mid(hanzi, j, 1)) And Asc(Mid(x, i, 1)) < Asc(Mid(hanzi, j + 1, 1)) Then temp = Mid(hanzi, 24 + j, 1) Next pinyin = pinyin & " " & temp i = i + 1 temp = "" Loop pinyin = LCase(pinyin) End Function 方法2: ======================= Option Explicit'在窗口中加两个TEXT控件,一个输入中文,一个显示英文 Private Sub Form_Load() Text1.Text = "我是中国人" End SubPrivate Sub Command1_Click() Text2.Text = GetPY(Text1.Text) End Sub'获得输入名称的首字拼音 Private Function GetPY(ByVal strParmeter As String) As String Dim intTmp As String, i As Long
For i = 1 To Len(strParmeter) intTmp = Asc(Mid(strParmeter, i, 1)) If intTmp < Asc("啊") Then GetPY = GetPY & "*" ElseIf intTmp >= Asc("啊") And intTmp < Asc("芭") Then GetPY = GetPY & "A" ElseIf intTmp >= Asc("芭") And intTmp < Asc("擦") Then GetPY = GetPY & "B" ElseIf intTmp >= Asc("擦") And intTmp < Asc("搭") Then GetPY = GetPY & "C" ElseIf intTmp >= Asc("搭") And intTmp < Asc("蛾") Then GetPY = GetPY & "D" ElseIf intTmp >= Asc("蛾") And intTmp < Asc("发") Then GetPY = GetPY & "E" ElseIf intTmp >= Asc("发") And intTmp < Asc("噶") Then GetPY = GetPY & "F" ElseIf intTmp >= Asc("噶") And intTmp < Asc("哈") Then GetPY = GetPY & "G" ElseIf intTmp >= Asc("哈") And intTmp < Asc("击") Then GetPY = GetPY & "H" ElseIf intTmp >= Asc("击") And intTmp < Asc("喀") Then GetPY = GetPY & "J" ElseIf intTmp >= Asc("喀") And intTmp < Asc("垃") Then GetPY = GetPY & "K" ElseIf intTmp >= Asc("垃") And intTmp < Asc("妈") Then GetPY = GetPY & "L" ElseIf intTmp >= Asc("妈") And intTmp < Asc("拿") Then GetPY = GetPY & "M" ElseIf intTmp >= Asc("拿") And intTmp < Asc("哦") Then GetPY = GetPY & "N" ElseIf intTmp >= Asc("哦") And intTmp < Asc("啪") Then GetPY = GetPY & "O" ElseIf intTmp >= Asc("啪") And intTmp < Asc("期") Then GetPY = GetPY & "P" ElseIf intTmp >= Asc("期") And intTmp < Asc("然") Then GetPY = GetPY & "Q" ElseIf intTmp >= Asc("然") And intTmp < Asc("撒") Then GetPY = GetPY & "R" ElseIf intTmp >= Asc("撒") And intTmp < Asc("塌") Then GetPY = GetPY & "S" ElseIf intTmp >= Asc("塌") And intTmp < Asc("挖") Then GetPY = GetPY & "T" ElseIf intTmp >= Asc("挖") And intTmp < Asc("昔") Then GetPY = GetPY & "W" ElseIf intTmp >= Asc("昔") And intTmp < Asc("压") Then GetPY = GetPY & "X" ElseIf intTmp >= Asc("压") And intTmp < Asc("匝") Then GetPY = GetPY & "Y" ElseIf intTmp >= Asc("匝") And intTmp < 0 Then GetPY = GetPY & "Z" ElseIf (intTmp >= 65 And intTmp <= 91) Or (intTmp >= 97 And intTmp <= 123) Then GetPY = GetPY & Mid(strParmeter, i, 1) Else GetPY = GetPY & "*" End If Next End Function
select pinyin from table1 where hanzi=N'孢'
select left(pinyin,1) from table1 where hanzi=N'孢'
create function f_ch2py(@chn nchar(1))
returns char(1)
as
begin
declare @n int
declare @c char(1)
set @n = 63select @n = @n +1,
@c = case chn when @chn then char(@n) else @c end
from(
select top 27 * from (
select chn =
'吖' union all select
'八' union all select
'嚓' union all select
'咑' union all select
'妸' union all select
'发' union all select
'旮' union all select
'铪' union all select
'丌' union all select --because have no 'i'
'丌' union all select
'咔' union all select
'垃' union all select
'嘸' union all select
'拏' union all select
'噢' union all select
'妑' union all select
'七' union all select
'呥' union all select
'仨' union all select
'他' union all select
'屲' union all select --no 'u'
'屲' union all select --no 'v'
'屲' union all select
'夕' union all select
'丫' union all select
'帀' union all select @chn) as a
order by chn COLLATE Chinese_PRC_CI_AS
) as b
return(@c)
endgo
select dbo.f_ch2py('斌') --B
select dbo.f_ch2py('国') --G
select dbo.f_ch2py('人') --R
select dbo.f_ch2py('镆') --M
drop function f_ch2py
Private Sub Command1_Click()
MsgBox pinyin("不到黄河心不死!")
End Sub
Function pinyin(ByVal x As String) As String
On Error Resume Next
Const hanzi = "啊芭擦搭蛾发噶哈击喀垃妈拿哦啪期然撒塌挖昔压匝座ABCDEFGHJKLMNOPQRSTWXYZZ"
pinyin = ""
Dim temp As String, i As Long, j As Long
i = 1
Do While i <= Len(x)
If Mid(x, i, 1) = "座" Then temp = "Z"
If InStr(",。“”;:?》《!·¥()", Mid(x, i, 1)) > 0 Then temp = ""
For j = 1 To 23
If Asc(Mid(x, i, 1)) >= Asc(Mid(hanzi, j, 1)) And Asc(Mid(x, i, 1)) < Asc(Mid(hanzi, j + 1, 1)) Then temp = Mid(hanzi, 24 + j, 1)
Next
pinyin = pinyin & " " & temp
i = i + 1
temp = ""
Loop
pinyin = LCase(pinyin)
End Function
方法2:
=======================
Option Explicit'在窗口中加两个TEXT控件,一个输入中文,一个显示英文
Private Sub Form_Load()
Text1.Text = "我是中国人"
End SubPrivate Sub Command1_Click()
Text2.Text = GetPY(Text1.Text)
End Sub'获得输入名称的首字拼音
Private Function GetPY(ByVal strParmeter As String) As String
Dim intTmp As String, i As Long
For i = 1 To Len(strParmeter)
intTmp = Asc(Mid(strParmeter, i, 1)) If intTmp < Asc("啊") Then
GetPY = GetPY & "*"
ElseIf intTmp >= Asc("啊") And intTmp < Asc("芭") Then
GetPY = GetPY & "A"
ElseIf intTmp >= Asc("芭") And intTmp < Asc("擦") Then
GetPY = GetPY & "B"
ElseIf intTmp >= Asc("擦") And intTmp < Asc("搭") Then
GetPY = GetPY & "C"
ElseIf intTmp >= Asc("搭") And intTmp < Asc("蛾") Then
GetPY = GetPY & "D"
ElseIf intTmp >= Asc("蛾") And intTmp < Asc("发") Then
GetPY = GetPY & "E"
ElseIf intTmp >= Asc("发") And intTmp < Asc("噶") Then
GetPY = GetPY & "F"
ElseIf intTmp >= Asc("噶") And intTmp < Asc("哈") Then
GetPY = GetPY & "G"
ElseIf intTmp >= Asc("哈") And intTmp < Asc("击") Then
GetPY = GetPY & "H"
ElseIf intTmp >= Asc("击") And intTmp < Asc("喀") Then
GetPY = GetPY & "J"
ElseIf intTmp >= Asc("喀") And intTmp < Asc("垃") Then
GetPY = GetPY & "K"
ElseIf intTmp >= Asc("垃") And intTmp < Asc("妈") Then
GetPY = GetPY & "L"
ElseIf intTmp >= Asc("妈") And intTmp < Asc("拿") Then
GetPY = GetPY & "M"
ElseIf intTmp >= Asc("拿") And intTmp < Asc("哦") Then
GetPY = GetPY & "N"
ElseIf intTmp >= Asc("哦") And intTmp < Asc("啪") Then
GetPY = GetPY & "O"
ElseIf intTmp >= Asc("啪") And intTmp < Asc("期") Then
GetPY = GetPY & "P"
ElseIf intTmp >= Asc("期") And intTmp < Asc("然") Then
GetPY = GetPY & "Q"
ElseIf intTmp >= Asc("然") And intTmp < Asc("撒") Then
GetPY = GetPY & "R"
ElseIf intTmp >= Asc("撒") And intTmp < Asc("塌") Then
GetPY = GetPY & "S"
ElseIf intTmp >= Asc("塌") And intTmp < Asc("挖") Then
GetPY = GetPY & "T"
ElseIf intTmp >= Asc("挖") And intTmp < Asc("昔") Then
GetPY = GetPY & "W"
ElseIf intTmp >= Asc("昔") And intTmp < Asc("压") Then
GetPY = GetPY & "X"
ElseIf intTmp >= Asc("压") And intTmp < Asc("匝") Then
GetPY = GetPY & "Y"
ElseIf intTmp >= Asc("匝") And intTmp < 0 Then
GetPY = GetPY & "Z"
ElseIf (intTmp >= 65 And intTmp <= 91) Or (intTmp >= 97 And intTmp <= 123) Then
GetPY = GetPY & Mid(strParmeter, i, 1)
Else
GetPY = GetPY & "*"
End If
Next
End Function