Delphi 从字符串中提取数字

function GetNumberFromStr(strIn: string; sFlag: string): string;
var
  i: Integer;
  tempStr: string;
begin
  tempStr := '';

  if Length(strIn) = 0 then
  begin
    Result := '';
    exit;
  end;

  for i := 1 to Length(strIn) do
  begin
    if strIn[i] = sFlag then   //截取到sFlag位置结束
      Break;

    if IsNumber(strIn[i]) then    //isNumber--System.Character
    begin
      tempStr := tempStr + strIn[i];
    end;
  end;

  Result := tempStr;
end;

如果是高版本Delphi 需引用System.Character
如果是在Delphi7,还需要一个判断是否是数子的函数

function IsNumber(N: string): Boolean;
var
  I: Integer;
begin
  Result := True;
  if Trim(N) = '' then
    Exit;

  if (Length(Trim(N)) > 1) and (Trim(N)[1] = '0') then
    Exit;

  for I := 1 to Length(N) do
  begin
    if not (N[I] in ['0'..'9']) then
    begin
      Result := False;
      Break;
    end;
  end;
end;

posted on   YXGust  阅读(1133)  评论(2编辑  收藏  举报

相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)

导航

点击右上角即可分享
微信分享提示