给你一个我收藏得函数吧(取第一个字母),来源我也忘了 function GetPYCode(HanStr: String) : String; const PRCCodePage=936; { 数据来源于汉字码表 } VowelPos: array['`'..'{'] of Integer = ($0000,$B0A1,$b0c5,$b2c1,$b4ee, $b6ea,$b7a2,$b8c1,$b9fe,$0000,$bbf7,$bfa8, $c0ac,$c2e8,$c4c3,$c5b6,$c5be,$c6da,$c8bb, $c8f6,$cbfa,$0000,$0000,$cdda,$cef4,$d1b9, $d4d1,$FFFF); var sVol : string; Vowels : String; i:Char; HanziCode:Word; lps,p1,p2:Pointer; begin sVol := HanStr; GetMem(lps,Length(sVol)+1); StrPCopy(lps,sVol); p1:=lps; p2:=CharNextEx(PRCCodePage,p1,0); Repeat if Abs(Longint(p2)-Longint(p1))=2 then begin HanziCode:=Word(p1^); HanziCode:=swap(HanziCode); for i:='`' to '{' do begin if VowelPos[i]>HanziCode then begin if i='a' then Vowels:=Vowels+i else if i='j' then //因为汉语内没有以“I”开头的拼音,遇到这种情况就是遇到了“H” Vowels:=Vowels+'h' else if i='w' then // 没有以“U、V”开头的拼音,遇到这种情况就是遇到了“T” Vowels:=Vowels+'t' else Vowels:=Vowels+Chr(Ord(i)-1); break; end; end; end else begin Vowels:=Vowels+PChar(p1)^; //非汉字不转换 end; p1:=p2; p2:=CharNextEx(PRCCodePage,p1,0); Until p1=p2; Result:=UpperCase(Vowels); FreeMem(lps); end;
我试过,可以的//此算法以Delphi语法编写 const ChinaCode: array[0..25,0..1] of Integer = ((1601,1636), (1637,1832), (1833,2077), (2078,2273),(2274,2301),(2302,2432),(2433,2593),(2594,2786),(9999,0000), (2787,3105),(3106,3211),(3212,3471),(3472,3634),(3635,3722),(3723,3729), (3730,3857),(3858,4026),(4027,4085),(4086,4389),(4390,4557),(9999,0000), (9999,0000),(4558,4683),(4684,4924),(4925,5248),(5249,5589));function HzPy(sr: String): String; var C1, Len1, C2: Integer; ir : Word; FResult : String; begin FResult := ''; C1 := 1; Len1 := Length(sr); while (C1<=Len1) do begin if (ord(sr[C1])>=160) and (ord(sr[C1+1])>=160) then begin ir := (ord(sr[C1])-160)*100 + ord(sr[C1+1])-160; C2 := 1; while (C2<=26) do begin if (ir>=ChinaCode[C2,0]) and (ir<=ChinaCode[C2,1]) then begin FResult := FResult+chr(C2+ord('a')); break; end; C2 := C2 + 1; end; end; C1 := C1 + 2; end; Result := FResult; end;//注:此算法中未考虑英文字母的转换 Length(sr)为取字符串长度 ord(sr[C1])为取字符ASCII码 chr(C2+ord('a'))生成相应ASCII码的字符
我试了试,没有发现特殊情况。 to 耙子兄,编码顺序在那里不是按拼音做的,谢谢!!!if edit1.Text < '吧' then showmessage('a') else if edit1.Text< '擦' then showmessage('b') else if edit1.text<'打' then showmessage('c') .....................
function GetCharInd(zzchar:string):char; // return a chinese's first letter begin case WORD(zzchar[1]) shl 8+WORD(zzchar[2]) of $B0A1..$B0C4:result:='A'; $B0C5..$B2C0:result:='B'; $B2C1..$B4ED:result:='C'; $B4EE..$B6E9:result:='D'; $B6EA..$B7A1:result:='E'; $B7A2..$B8C0:result:='F'; $B8C1..$B9FD:result:='G'; $B9FE..$BBF6:result:='H'; $BBF7..$BFA5:result:='J'; $BFA6..$C0AB:result:='K'; $C0AC..$C2E7:result:='L'; $C2E8..$C4C2:result:='M'; $C4C3..$C5B5:result:='N'; $C5B6..$C5BD:result:='O'; $C5BE..$C6D9:result:='P'; $C6DA..$C8BA:result:='Q'; $C8BB..$C8F5:result:='R'; $C8F6..$CBF9:result:='S'; $CBFA..$CDD9:result:='T'; $CDDA..$CEF3:result:='W'; $CEF4..$D188:result:='X'; $D1B9..$D4D0:result:='Y'; $D4D1..$D7F9:result:='Z'; else result:=#0; end; end;
function GetPYCode(HanStr: String) : String;
const
PRCCodePage=936;
{ 数据来源于汉字码表 }
VowelPos: array['`'..'{'] of Integer = ($0000,$B0A1,$b0c5,$b2c1,$b4ee,
$b6ea,$b7a2,$b8c1,$b9fe,$0000,$bbf7,$bfa8,
$c0ac,$c2e8,$c4c3,$c5b6,$c5be,$c6da,$c8bb,
$c8f6,$cbfa,$0000,$0000,$cdda,$cef4,$d1b9,
$d4d1,$FFFF);
var
sVol : string;
Vowels : String;
i:Char;
HanziCode:Word;
lps,p1,p2:Pointer;
begin
sVol := HanStr;
GetMem(lps,Length(sVol)+1);
StrPCopy(lps,sVol);
p1:=lps;
p2:=CharNextEx(PRCCodePage,p1,0);
Repeat
if Abs(Longint(p2)-Longint(p1))=2 then
begin
HanziCode:=Word(p1^);
HanziCode:=swap(HanziCode);
for i:='`' to '{' do
begin
if VowelPos[i]>HanziCode then
begin
if i='a' then
Vowels:=Vowels+i
else if i='j' then //因为汉语内没有以“I”开头的拼音,遇到这种情况就是遇到了“H”
Vowels:=Vowels+'h'
else if i='w' then // 没有以“U、V”开头的拼音,遇到这种情况就是遇到了“T”
Vowels:=Vowels+'t'
else
Vowels:=Vowels+Chr(Ord(i)-1);
break;
end;
end;
end
else begin
Vowels:=Vowels+PChar(p1)^; //非汉字不转换
end;
p1:=p2;
p2:=CharNextEx(PRCCodePage,p1,0);
Until p1=p2;
Result:=UpperCase(Vowels);
FreeMem(lps);
end;
const ChinaCode: array[0..25,0..1] of Integer = ((1601,1636), (1637,1832),
(1833,2077),
(2078,2273),(2274,2301),(2302,2432),(2433,2593),(2594,2786),(9999,0000),
(2787,3105),(3106,3211),(3212,3471),(3472,3634),(3635,3722),(3723,3729),
(3730,3857),(3858,4026),(4027,4085),(4086,4389),(4390,4557),(9999,0000),
(9999,0000),(4558,4683),(4684,4924),(4925,5248),(5249,5589));function HzPy(sr: String): String;
var
C1, Len1, C2: Integer;
ir : Word;
FResult : String;
begin
FResult := '';
C1 := 1;
Len1 := Length(sr);
while (C1<=Len1) do
begin
if (ord(sr[C1])>=160) and (ord(sr[C1+1])>=160) then
begin
ir := (ord(sr[C1])-160)*100 + ord(sr[C1+1])-160;
C2 := 1;
while (C2<=26) do
begin
if (ir>=ChinaCode[C2,0]) and (ir<=ChinaCode[C2,1]) then
begin
FResult := FResult+chr(C2+ord('a'));
break;
end;
C2 := C2 + 1;
end;
end;
C1 := C1 + 2;
end;
Result := FResult;
end;//注:此算法中未考虑英文字母的转换
Length(sr)为取字符串长度
ord(sr[C1])为取字符ASCII码
chr(C2+ord('a'))生成相应ASCII码的字符
to 耙子兄,编码顺序在那里不是按拼音做的,谢谢!!!if edit1.Text < '吧' then showmessage('a')
else
if edit1.Text< '擦' then showmessage('b')
else
if edit1.text<'打' then showmessage('c')
.....................
begin
case WORD(zzchar[1]) shl 8+WORD(zzchar[2]) of
$B0A1..$B0C4:result:='A';
$B0C5..$B2C0:result:='B';
$B2C1..$B4ED:result:='C';
$B4EE..$B6E9:result:='D';
$B6EA..$B7A1:result:='E';
$B7A2..$B8C0:result:='F';
$B8C1..$B9FD:result:='G';
$B9FE..$BBF6:result:='H';
$BBF7..$BFA5:result:='J';
$BFA6..$C0AB:result:='K';
$C0AC..$C2E7:result:='L';
$C2E8..$C4C2:result:='M';
$C4C3..$C5B5:result:='N';
$C5B6..$C5BD:result:='O';
$C5BE..$C6D9:result:='P';
$C6DA..$C8BA:result:='Q';
$C8BB..$C8F5:result:='R';
$C8F6..$CBF9:result:='S';
$CBFA..$CDD9:result:='T';
$CDDA..$CEF3:result:='W';
$CEF4..$D188:result:='X';
$D1B9..$D4D0:result:='Y';
$D4D1..$D7F9:result:='Z';
else
result:=#0;
end;
end;