这是一个取汉字拼音首字母的函数,但不知为什么一碰到"陈登华文选集"这几个汉字就死循环,帮我看看错误出在那里,谢谢!下面是这个函数!function ChineseToSpell(Chinese: string): string;
  function GetPYIndexChar(hzchar: string): Char;
  begin
    case Word(hzchar[1]) shl 8 + Word(hzchar[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 := Char(32);
    end;
  end;
var
  i : Integer;
  C : Char;
  Spell: string;
begin
  Spell := '';
  i := 1;
  while i <= Length(Chinese) do
  begin
    if Chinese[i] <= Chr(127) then
    begin
      Spell := Spell + UpCase(Chinese[i]);
      i := i + 1;
    end
    else
    begin
      C := GetPYIndexChar(Copy(Chinese, i, 2));
      if C <> Char(32) then
      begin
        Spell := Spell + UpCase(C);
        i := i + 2;
      end;
    end;
  end;
  Result := Spell;
end;
这是调用这个函数:
procedure TForm1.Button1Click(Sender: TObject);
var
  Spell : string;
begin
  Spell := ChineseToSpell('陈登华文选集');
end;

解决方案 »

  1.   

    也不给你检查了,直接贴给你有用的,第二个在第二个你的另一个贴子:
    1.想要大写改成大写字母好了:
    function GetEn(CnString: string): string;
      function GetEnChar(cnchar: string): char;
      begin
        case Word(cnchar[1]) shl 8 + Word(cnchar[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 := char(0);
        end;
      end;
    var
      i, len: integer;
    begin
      CnString := StringReplace(CnString, ' ', '', [rfReplaceAll]);  //删除空格
      len := Length(CnString) - Length(WideString(CnString));        //汉字个数
      for i := 1 to len do
        result := result + GetEnChar(copy(CnString, 2*i-1, 2));
    end;
      

  2.   

    经过测试与改进,你的错误是:
          C := GetPYIndexChar(Copy(Chinese, i, 2));
          if C <> Char(32) then {在这里;若不成立,下面的i := i + 2执行不到,成死循环}
          begin
            Spell := Spell + UpCase(C);
            i := i + 2;
          end;
    要改成
          C := GetPYIndexChar(Copy(Chinese, i, 2));
          if C <> Char(32) then
            Spell := Spell + UpCase(C);
          i := i + 2;