15

Delphi 7

How do i remove leading zeros in a delphi string?

Example:

00000004357816

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

6 Answers 6

24

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;
Sign up to request clarification or add additional context in comments.

5 Comments

This is what I would have done. Much more efficient than David's answer.
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.
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.
+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.
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);
12
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.

8 Comments

what if there is a zero in the string (not necessary in the beginning? Example: 00000004307016
That's fine, the code just removes leading zeros as you requested. Your example will return '4307016' from this code.
@Shane, I think you should read the code once more. It finds the first non-0 char and returns the rest of the string.
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
Hmm... ShowMessage(removeLeadingZeros('000'));
|
11

Use JEDI Code Library to do this:

uses JclStrings;

var
  S: string;

begin
  S := StrTrimCharLeft('00000004357816', '0');
end.

1 Comment

You should probably mention (in case the OP doesn't know) that this requires the JEDI Code Library (JCL), and provide a link.
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.

1 Comment

Also only if the string is actually a number. I.e. no alpha chars at all.
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

1 Comment

This should be the accepted answer as of 2021
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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.