首页 > 其他分享 >Delphi 从字符串中提取数字

Delphi 从字符串中提取数字

时间:2022-09-28 09:12:37浏览次数:48  
标签:begin end string strIn Delphi Length tempStr 字符串 提取

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;

标签:begin,end,string,strIn,Delphi,Length,tempStr,字符串,提取
From: https://www.cnblogs.com/YXGust/p/16736795.html

相关文章