方法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
--SQL数据库就简单了. --创建取汉字首字拼音的函数 create function fGetPy(@Str varchar(500)='') returns varchar(500) as begin declare @strlen int,@return varchar(500),@ii int select @strlen=len(@str),@return='',@ii=0 set @ii=0 while @ii<@strlen begin set @ii=@ii+1 select @return=@return+py from (select 0xB0A1 as cbegin, 0xB0C4 as cend,'A' as py union all select 0xB0C5, 0xB2C0,'B' union all select 0xB2C1, 0xB4ED,'C' union all select 0xB4EE, 0xB6E9,'D' union all select 0xB6EA, 0xB7A1,'E' union all select 0xB7A2, 0xB8C0,'F' union all select 0xB8C1, 0xB9FD,'G' union all select 0xB9FE, 0xBBF6,'H' union all select 0xBBF7, 0xBFA5,'J' union all select 0xBFA6, 0xC0AB,'K' union all select 0xC0AC, 0xC2E7,'L' union all select 0xC2E8, 0xC4C2,'M' union all select 0xC4C3, 0xC5B5,'N' union all select 0xC5B6, 0xC5BD,'O' union all select 0xC5BE, 0xC6D9,'P' union all select 0xC6DA, 0xC8BA,'Q' union all select 0xC8BB, 0xC8F5,'R' union all select 0xC8F6, 0xCBF9,'S' union all select 0xCBFA, 0xCDD9,'T' union all select 0xCDDA, 0xCEF3,'W' union all select 0xCEF4, 0xD1B8,'X' union all select 0xD1B9, 0xD4D0,'Y' union all select 0xD4D1, 0xD7F9,'Z') a where cast(substring(@str,@ii,1) as varbinary) between cbegin and cend end return(@return) end go --调用测试 select dbo.fgetpy('我是一个兵') as 我是一个兵,dbo.fgetpy('中国人') as 中国人 go--删除拼音函数 drop function fgetpy
--如果是要得到汉字的拼音,就要一个拼音库了./*--获得汉字拼音的函数 需要创建一个拼音表,包含所有汉字的发音,这个可以通过转换全拼输入法的编码库得到,这里仅举了一个简单的例子. --邹建 2003.10--*/--创建汉字拼音库 create table YingShe(CHR char(2),PY varchar(10)) insert YingShe select '长','chang' union all select '长','zhang' union all select '城','cheng' union all select '科','kel' union all select '技','ji' union all select '金','jin' union all select '立','li' union all select '章','zhang' union all select '公','gong' union all select '司','si'/*--下面是两个函数,一个以表的形式返回某个字符串的全部拼音,一个返回某某个字符串的其中一个拼音 --*/go --获取汉字拼音的函数--返回所有的拼音 create function f_getpy_tb(@str varchar(100)) returns @tb table(re varchar(8000)) as begin declare @re table(id int,re varchar(8000)) --数据处理中间表declare @i int,@ilen int,@splitchr varchar(1) select @splitchr=' ' --两个拼音之间的分隔符(目的是为了通用性考虑) ,@i=1,@ilen=len(@str)insert into @re select @i,py from YingShe where chr=substring(@str,@i,1) while @i<@ilen begin set @i=@i+1 insert into @re select @i,re+@splitchr+py from @re a,YingShe b where a.id=@i-1 and b.chr=substring(@str,@i,1) endinsert into @tb select re from @re where id=@i return end go--获取汉字拼音的函数--返回汉字的某一个拼音 create function f_getpy(@str varchar(100)) returns varchar(8000) as begin declare @re varchar(8000)declare @i int,@ilen int,@splitchr varchar(1) select @splitchr=' ' --两个拼音之间的分隔符(目的是为了通用性考虑) ,@i=1,@ilen=len(@str)select @re=py from YingShe where chr=substring(@str,@i,1) while @i<@ilen begin set @i=@i+1 select top 1 @re=@re+@splitchr+py from YingShe where chr=substring(@str,@i,1) endreturn(@re) end go--测试 --返回'长城'的所有可能拼音 select * from dbo.f_getpy_tb('长城')--应用案例:create table MyTable(Name varchar(10),ID int) insert MyTable select '长城科技',1 union all select '金长城科技',2 union all select '立章城公司',3go--查询包含与'长城'拼音一样的记录: select * from mytable where exists(select 1 from dbo.f_getpy_tb('长城') where dbo.f_getpy(name) like '%'+re+'%')--删除表 drop table YingShe,mytable --删除拼音函数 drop function f_getpy,f_getpy_tb
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
--创建取汉字首字拼音的函数
create function fGetPy(@Str varchar(500)='')
returns varchar(500)
as
begin
declare @strlen int,@return varchar(500),@ii int
select @strlen=len(@str),@return='',@ii=0
set @ii=0
while @ii<@strlen
begin
set @ii=@ii+1
select @return=@return+py
from (select 0xB0A1 as cbegin, 0xB0C4 as cend,'A' as py
union all select 0xB0C5, 0xB2C0,'B'
union all select 0xB2C1, 0xB4ED,'C'
union all select 0xB4EE, 0xB6E9,'D'
union all select 0xB6EA, 0xB7A1,'E'
union all select 0xB7A2, 0xB8C0,'F'
union all select 0xB8C1, 0xB9FD,'G'
union all select 0xB9FE, 0xBBF6,'H'
union all select 0xBBF7, 0xBFA5,'J'
union all select 0xBFA6, 0xC0AB,'K'
union all select 0xC0AC, 0xC2E7,'L'
union all select 0xC2E8, 0xC4C2,'M'
union all select 0xC4C3, 0xC5B5,'N'
union all select 0xC5B6, 0xC5BD,'O'
union all select 0xC5BE, 0xC6D9,'P'
union all select 0xC6DA, 0xC8BA,'Q'
union all select 0xC8BB, 0xC8F5,'R'
union all select 0xC8F6, 0xCBF9,'S'
union all select 0xCBFA, 0xCDD9,'T'
union all select 0xCDDA, 0xCEF3,'W'
union all select 0xCEF4, 0xD1B8,'X'
union all select 0xD1B9, 0xD4D0,'Y'
union all select 0xD4D1, 0xD7F9,'Z') a
where cast(substring(@str,@ii,1) as varbinary)
between cbegin and cend
end
return(@return)
end
go
--调用测试
select dbo.fgetpy('我是一个兵') as 我是一个兵,dbo.fgetpy('中国人') as 中国人
go--删除拼音函数
drop function fgetpy
--邹建 2003.10--*/--创建汉字拼音库
create table YingShe(CHR char(2),PY varchar(10))
insert YingShe
select '长','chang'
union all select '长','zhang'
union all select '城','cheng'
union all select '科','kel'
union all select '技','ji'
union all select '金','jin'
union all select '立','li'
union all select '章','zhang'
union all select '公','gong'
union all select '司','si'/*--下面是两个函数,一个以表的形式返回某个字符串的全部拼音,一个返回某某个字符串的其中一个拼音
--*/go
--获取汉字拼音的函数--返回所有的拼音
create function f_getpy_tb(@str varchar(100))
returns @tb table(re varchar(8000))
as
begin
declare @re table(id int,re varchar(8000)) --数据处理中间表declare @i int,@ilen int,@splitchr varchar(1)
select @splitchr=' ' --两个拼音之间的分隔符(目的是为了通用性考虑)
,@i=1,@ilen=len(@str)insert into @re select @i,py from YingShe where chr=substring(@str,@i,1)
while @i<@ilen
begin
set @i=@i+1
insert into @re select @i,re+@splitchr+py from @re a,YingShe b
where a.id=@i-1 and b.chr=substring(@str,@i,1)
endinsert into @tb select re from @re where id=@i
return
end
go--获取汉字拼音的函数--返回汉字的某一个拼音
create function f_getpy(@str varchar(100))
returns varchar(8000)
as
begin
declare @re varchar(8000)declare @i int,@ilen int,@splitchr varchar(1)
select @splitchr=' ' --两个拼音之间的分隔符(目的是为了通用性考虑)
,@i=1,@ilen=len(@str)select @re=py from YingShe where chr=substring(@str,@i,1)
while @i<@ilen
begin
set @i=@i+1
select top 1 @re=@re+@splitchr+py
from YingShe where chr=substring(@str,@i,1)
endreturn(@re)
end
go--测试
--返回'长城'的所有可能拼音
select * from dbo.f_getpy_tb('长城')--应用案例:create table MyTable(Name varchar(10),ID int)
insert MyTable select '长城科技',1
union all select '金长城科技',2
union all select '立章城公司',3go--查询包含与'长城'拼音一样的记录:
select * from mytable
where exists(select 1 from dbo.f_getpy_tb('长城') where dbo.f_getpy(name) like '%'+re+'%')--删除表
drop table YingShe,mytable
--删除拼音函数
drop function f_getpy,f_getpy_tb
http://expert.csdn.net/Expert/topic/2361/2361465.xml?temp=.2815821