开发者

How can I strip leading zeros from a hexadecimal string?

开发者 https://www.devze.com 2023-04-12 00:11 出处:网络
If I have a string as \'0000FFFF\' or \'0000F0F0\', how can have the output be respectively \'FFFF\' an开发者_运维问答d \'F0F0\', deleting the non-significant 0 from it?This function will strip leadin

If I have a string as '0000FFFF' or '0000F0F0', how can have the output be respectively 'FFFF' an开发者_运维问答d 'F0F0', deleting the non-significant 0 from it?


This function will strip leading zeros:

function StripLeadingZeros(const s: string): string;
var
  i, Len: Integer;
begin
  Len := Length(s);
  for i := 1 to Len do begin
    if s[i]<>'0' then begin
      Result := Copy(s, i, Len);
      exit;
    end;
  end;
  Result := '0';
end;


Format('%X', [StrToInt('$' + number)])


function mystrip(Value: string): string; 
var 
  Flag: Boolean; 
  Index: Integer; 
begin 
  Result := ''; Flag := false; 
  for Index := 1 to Length(Value) do 
  begin 
    if not Flag then 
    begin 
      if (Value[Index] <> #48) then 
      begin 
        Flag := true; 
        Result := Result + Value[Index]; 
      end 
    end 
  else 
    Result := Result + Value[Index]; 
  end; 
end; 


This problem splits into two parts and solves in 4 LoC (or 5 with explaining variable).

licensed under GPL

function TrimLeading(const S: string): string;
var
  I: Integer;
begin
  I := 1;
  while (I < Length(S)) and (S[I] = '0') do
    Inc(I);
  Result := Copy(S, I, MaxInt);
end;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号