检查MyString [1]是否是字母字符?

我有一个字符串,我们称之为MyStr.我试图摆脱字符串中的每个非字母字符.就像在IM中像MSN和Skype一样,人们将他们的显示名称设置为[-Bobby-].我想删除该字符串中不是字母字符的所有内容,所以我留下的就是“名称”.

我怎么能在Delphi中做到这一点?我正在考虑创建一个TStringlist并将每个有效字符存储在那里,然后使用IndexOf检查char是否有效,但我希望有一种更简单的方法.

解决方法

最简单的方法
function GetAlphaSubstr(const Str: string): string;
const
  ALPHA_CHARS = ['a'..'z','A'..'Z'];
var
  ActualLength: integer;
  i: Integer;
begin
  SetLength(result,length(Str));
  ActualLength := 0;
  for i := 1 to length(Str) do
    if Str[i] in ALPHA_CHARS then
    begin
      inc(ActualLength);
      result[ActualLength] := Str[i];
    end;
  SetLength(Result,ActualLength);
end;

但这只会将英文字母视为“字母字符”.它甚至不会将极其重要的瑞典字母Å,Ä和Ö视为“字母字符”!

稍微复杂一点

function GetAlphaSubstr2(const Str: string): string;
var
  ActualLength: integer;
  i: Integer;
begin
  SetLength(result,length(Str));
  ActualLength := 0;
  for i := 1 to length(Str) do
    if Character.IsLetter(Str[i]) then
    begin
      inc(ActualLength);
      result[ActualLength] := Str[i];
    end;
  SetLength(Result,ActualLength);
end;

相关文章

 从网上看到《Delphi API HOOK完全说明》这篇文章,基本上都...
  从网上看到《Delphi API HOOK完全说明》这篇文章,基本上...
ffmpeg 是一套强大的开源的多媒体库 一般都是用 c/c+&#x...
32位CPU所含有的寄存器有:4个数据寄存器(EAX、EBX、ECX和ED...
1 mov dst, src dst是目的操作数,src是源操作数,指令实现的...