定义一个SplitStr函数,参数是文本框中的字符串,使用ByteType函数返回这个字符串中每个字符所占的字节数,如果返回mbSingleByte(单字节),那么说明这个字符是字母或数字,如果是双字节,那么说明这个字符是汉字。根据字符的字节数通过copy函数获取当前这个字符,然后调用GetSpell函数来返回这个字符的拼音简码。主要代码如下:
function TForm1.SplitStr(str: string): string;
var
text: String;
i, Count: Integer;
begin
text := '';
i := 1;
while i <= Length(str) do
begin
if ByteType(str, i) = mbSingleByte then
Count := 1
else
Count := 2;
text := text + GetSpell(copy(str, i, count));
i := i + count;
end;
Result := text;
end;
function TForm1.SplitStr(str: string): string;
var
text: String;
i, Count: Integer;
begin
text := '';
i := 1;
while i <= Length(str) do
begin
if ByteType(str, i) = mbSingleByte then
Count := 1
else
Count := 2;
text := text + GetSpell(copy(str, i, count));
i := i + count;
end;
Result := text;
end;