removing leading zeros from delphi string
Asked Answered
S

6

15

Delphi 7

How do i remove leading zeros in a delphi string?

Example:

00000004357816

function removeLeadingZeros(ValueStr: String): String
begin
 result:= 
end;
Seedbed answered 2/5, 2011 at 12:8 Comment(0)
A
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.

Airwaves answered 2/5, 2011 at 12:13 Comment(8)
what if there is a zero in the string (not necessary in the beginning? Example: 00000004307016Seedbed
That's fine, the code just removes leading zeros as you requested. Your example will return '4307016' from this code.Airwaves
@Shane, I think you should read the code once more. It finds the first non-0 char and returns the rest of the string.Lafreniere
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 testSeedbed
Hmm... ShowMessage(removeLeadingZeros('000'));Roentgenology
Wow David, you wrote it and didn't try it? :-)Weekley
@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. ;-)Airwaves
I'm not sure Copy(Value,i) will compile with Delphi 7. Perhaps you should need to write Copy(Value,i,maxInt)Doane
R
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;
Roentgenology answered 2/5, 2011 at 13:59 Comment(5)
This is what I would have done. Much more efficient than David's answer.Subinfeudate
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.Kitti
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.Aeolus
+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.Airwaves
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);Luge
A
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.

Airwaves answered 2/5, 2011 at 12:13 Comment(8)
what if there is a zero in the string (not necessary in the beginning? Example: 00000004307016Seedbed
That's fine, the code just removes leading zeros as you requested. Your example will return '4307016' from this code.Airwaves
@Shane, I think you should read the code once more. It finds the first non-0 char and returns the rest of the string.Lafreniere
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 testSeedbed
Hmm... ShowMessage(removeLeadingZeros('000'));Roentgenology
Wow David, you wrote it and didn't try it? :-)Weekley
@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. ;-)Airwaves
I'm not sure Copy(Value,i) will compile with Delphi 7. Perhaps you should need to write Copy(Value,i,maxInt)Doane
S
11

Use JEDI Code Library to do this:

uses JclStrings;

var
  S: string;

begin
  S := StrTrimCharLeft('00000004357816', '0');
end.
Saltcellar answered 2/5, 2011 at 12:30 Comment(1)
You should probably mention (in case the OP doesn't know) that this requires the JEDI Code Library (JCL), and provide a link.Ison
H
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.

Herron answered 2/5, 2011 at 12:19 Comment(1)
Also only if the string is actually a number. I.e. no alpha chars at all.Lotetgaronne
K
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

Kekkonen answered 12/8, 2021 at 17:57 Comment(1)
This should be the accepted answer as of 2021Magnuson
L
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;
Londonderry answered 19/7, 2020 at 17:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.