removing leading zeros from delphi string

IElite picture IElite · May 2, 2011 · Viewed 8.8k times · Source

Delphi 7

How do i remove leading zeros in a delphi string?

Example:

00000004357816

function removeLeadingZeros(ValueStr: String): String
begin
 result:= 
end;

Answer

kludg picture kludg · May 2, 2011

Code that removes leading zeroes from '000'-like strings correctly:

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