加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 10:06:41 所属栏目:大数据 来源:网络整理
导读:我有一个字符串,我们称之为MyStr.我试图摆脱字符串中的每个非字母字符.就像在IM中像MSN和Skype一样,人们将他们的显示名称设置为[-Bobby-].我想删除该字符串中不是字母字符的所有内容,所以我留下的就是“名称”. 我怎么能在Delphi中做到这一点?我正在考虑创
我有一个字符串,我们称之为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;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读