14

Delphi 7

How do i remove leading zeros in a delphi string?

Example:

00000004357816

function removeLeadingZeros(ValueStr: String): String
begin
 result:= 
end;
IElite
  • 1,818
  • 9
  • 39
  • 64

6 Answers6

22

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;
kludg
  • 27,213
  • 5
  • 67
  • 118
  • This is what I would have done. Much more efficient than David's answer. – lkessler May 02 '11 at 14:14
  • 1
    Nice solution. From a practicality standpoint, it will be more useful if the trim char ('0' in this case) is passed in as a parameter, then it can trim any char. – Chris Thornton May 02 '11 at 14:32
  • I am not sure if this is "much more efficient than David's answer", but it is slightly more elegant and, most importantly, doesn't show the bug in David's code. – Andreas Rejbrand May 02 '11 at 15:04
  • 1
    +1 I wouldn't worry too much about efficiency. Getting it right is more important! For what it's worth there's no real difference in efficiency. – David Heffernan May 02 '11 at 15:44
  • Calling this function for `0000.89` results in `.89` but `0.89` would be better. So I think the line no. 7 could be changed to: while (I < L) and (S[I] = '0') and (S[I+1] = '0') do Inc(I); – datatraveller1 Dec 03 '22 at 21:55
11
function removeLeadingZeros(const Value: string): string;
var
  i: Integer;
begin
  for i := 1 to Length(Value) do
    if Value[i]<>'0' then
    begin
      Result := Copy(Value, i, MaxInt);
      exit;
    end;
  Result := '';
end;

Depending on the exact requirements you may wish to trim whitespace. I have not done that here since it was not mentioned in the question.

Update

I fixed the bug that Serg identified in the original version of this answer.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • what if there is a zero in the string (not necessary in the beginning? Example: 00000004307016 – IElite May 02 '11 at 12:19
  • That's fine, the code just removes leading zeros as you requested. Your example will return '4307016' from this code. – David Heffernan May 02 '11 at 12:22
  • @Shane, I think you should read the code once more. It finds the first non-0 char and returns the rest of the string. – Jørn E. Angeltveit May 02 '11 at 12:23
  • I read it, i didn't understand it. I made my comment then i tested it. It errored out a couple of times cause of missing semi-colons, and the addition of an extra "End"....but i figured it out and got it to compile and was able to test – IElite May 02 '11 at 12:43
  • 6
    Hmm... ShowMessage(removeLeadingZeros('000')); – kludg May 02 '11 at 13:22
  • Wow David, you wrote it and didn't try it? :-) – Warren P May 02 '11 at 20:46
  • @Warren yeah, silly me. I just thought I could write perfect code without testing it. Wrong again! Of course it would have been easy for @Serg to have fixed it with an edit to my post. ;-) – David Heffernan May 02 '11 at 21:17
  • I'm not sure `Copy(Value,i)` will compile with Delphi 7. Perhaps you should need to write `Copy(Value,i,maxInt)` – Arnaud Bouchez May 04 '11 at 06:37
11

Use JEDI Code Library to do this:

uses JclStrings;

var
  S: string;

begin
  S := StrTrimCharLeft('00000004357816', '0');
end.
  • 6
    You should probably mention (in case the OP doesn't know) that this requires the JEDI Code Library (JCL), and provide a link. – Ken White May 02 '11 at 12:40
8

Probably not the fastest one, but it's a one-liner ;-)

function RemoveLeadingZeros(const aValue: String): String;
begin
  Result := IntToStr(StrToIntDef(aValue,0));
end;

Only works for numbers within the Integer range, of course.

Jørn E. Angeltveit
  • 3,029
  • 3
  • 22
  • 53
5

Searching for this in 2021 there is a much better solution that I found now and that is using the TStringHelper function TrimLeft as follows

myString := myString.TrimLeft(['0']);

see here for more on the documentation http://docwiki.embarcadero.com/Libraries/Sydney/en/System.SysUtils.TStringHelper.TrimLeft

StevenW
  • 51
  • 1
  • 2
0

Try this:

function TFrmMain.removeLeadingZeros(const yyy: string): string;
var
  xxx : string;
begin
  xxx:=yyy;
   while LeftStr(xxx,1) = '0' do
    begin
      Delete(xxx,1,1);
    end;
    Result:=xxx;
end;
Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39