gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 14:49:31 24 4
gpt4 key购买 nike

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

我怎样才能在德尔福中做到这一点?我正在考虑创建一个 TStringlist 并将每个有效字符存储在其中,然后使用 IndexOf 检查该字符是否有效,但我希望有一种更简单的方法。

最佳答案

最简单的方法是

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;

关于string - 检查 MyString[1] 是否是字母字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5171784/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com