Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to remove leading zero

515 views
Skip to first unread message

Wasis Sugiono

unread,
Mar 9, 1999, 3:00:00 AM3/9/99
to
How can I remove leading zeros of a string?

For example:
'000' --> '0'
'001' --> '1'
'010' --> '10'
'100' --> '100'
'127.000.000.001' --> '127.0.0.1'

Thanks in advance.

David

unread,
Mar 9, 1999, 3:00:00 AM3/9/99
to
What about this:

function RemoveLeadingZeros(const S: String): String;
var
sTmp: String;
i: Integer;
begin
for i := 1 to Length(S) do
begin
if (S[i] = '.') or (i = Length(S)) then
begin
if i < Length(S) then
Result := Result + FormatFloat('##0', StrToFloat(sTmp)) + '.'
else
Result := Result + FormatFloat('##0', StrToFloat(sTmp + S[i]));
sTmp := '';
end
else
sTmp := sTmp + S[i];
end;
end;

David.

Brendan Delumpa

unread,
Mar 9, 1999, 3:00:00 AM3/9/99
to
I use this all the time for formatting imported flat files:

function StripFrontChars(S : String; ch : Char) : String;
begin
while (S[1] = ch) and (Length(S) > 0) do
S := Copy(S, 2, Length(S) - 1);
Result := S;
end;

--
Brendan
===========================================
If you think you have all the answers,
you haven't thought of all the questions...
===========================================

Wasis Sugiono wrote in message <36E4CA66...@rad.net.id>...

Philippe Ranger

unread,
Mar 9, 1999, 3:00:00 AM3/9/99
to
Wazis: >>

How can I remove leading zeros of a string?

For example:
'000' --> '0'
'001' --> '1'
'010' --> '10'
'100' --> '100'
'127.000.000.001' --> '127.0.0.1'
<<

Function unZero (s: string): string;
Var
j, j0: integer;
qDel0: boolean;
Begin
qDel0 := true;
j := 1;
while (j <= length(s)) do begin
case s[j] of
'1'..'9': qDel0 := false;
'0': if qDel0 then begin
j0 := j;
while (j < length(s)) and
(s[j+1] in ['0'..'9']) and
(s[j] = '0')
do inc(j);
delete(s, j0, j-j0);
end;
else qDel0 := true;
end;
inc(j);
end;
result := s;
End;

PhR


Eyal Post

unread,
Mar 9, 1999, 3:00:00 AM3/9/99
to
I would use

s:=IntToStr(StrToInt(s));

for the first 4 examples.
For the 5th - use the answers above.

Eyal

Wasis Sugiono wrote:
>
> How can I remove leading zeros of a string?
>
> For example:
> '000' --> '0'
> '001' --> '1'
> '010' --> '10'
> '100' --> '100'
> '127.000.000.001' --> '127.0.0.1'
>

> Thanks in advance.

--
Eyal Post
mailto:ey...@ladpc.gov.il
mailto:eyal...@geocities.com
------------

Peter Below (TeamB)

unread,
Mar 9, 1999, 3:00:00 AM3/9/99
to
In article <36E4CA66...@rad.net.id>, Wasis Sugiono wrote:
> How can I remove leading zeros of a string?
>
> For example:
> '000' --> '0'
> '001' --> '1'
> '010' --> '10'
> '100' --> '100'
>

Function RemoveLeadingZeros( Const S: String ): String;
Begin
Result := S;
While (Length(Result) > 1) and (Result[1] = '0') Do
Delete(Result, 1, 1 );
End;

> '127.000.000.001' --> '127.0.0.1'

This is a completely different problem, you could write a function that
takes the string apart into the numeric substring, passes each
substring to RemoveLeadingZeros and assembles the resulting
substrings again.

Function RemoveExtraZerosFromIPAddress( S: String ):String;
Var
dotpos: Integer;
Begin
Result := EmptyStr;
Repeat
dotpos := Pos('.', S);
If dotpos = 0 Then
Result := Result + S
Else Begin
Result := Result + RemoveLeadingZeros( Copy(S, 1, dotpos-1))
+ '.';
Delete(S, 1, dotpos);
End;
Until dotpos = 0;
End;


Peter Below (TeamB) 10011...@compuserve.com)
No e-mail responses, please, unless explicitely requested!


Mike Orriss

unread,
Mar 10, 1999, 3:00:00 AM3/10/99
to
In article <7c3rp9$h2...@forums.borland.com>, Brendan Delumpa wrote:
> while (S[1] = ch) and (Length(S) > 0) do
>

Doesn't that give you a problem?

I would do it the other way round:

while ((Length(S) > 0) and (S[1] = ch) do


Mike Orriss (m...@3kcc.co.uk)
http://www.3kcc.co.uk/notetree.htm


Philippe Ranger

unread,
Mar 10, 1999, 3:00:00 AM3/10/99
to
Wazis: >>

How can I remove leading zeros of a string?
<<

Yesterday's code, simplified —

Function unZero (s: string): string;

(*Returns s with zeros removed iff they both come at the beginning or after
a non-digit, and precede a digit (0 included).*)
Var
j: integer;
nDel: integer; (*number of chars to delete in current run of 0s
-1 means we cannot delete the first 0 we meet*)
Begin
nDel := -1;
for j := length(s) downto 1 do case s[j] of
'0': inc(nDel);
'1'..'9': nDel := 0;
else begin
if (nDel > 0) then delete(s, j+1, nDel);
nDel := -1;
end;
end;
if (nDel > 0) then delete(s, 1, nDel);

Ernie Deel

unread,
Mar 10, 1999, 3:00:00 AM3/10/99
to
Peter Below (TeamB) <10011...@compuXXserve.com> wrote in
message ...

> Function RemoveExtraZerosFromIPAddress( S: String ):String;
> Var
> dotpos: Integer;
> Begin
> Result := EmptyStr;
> Repeat
> dotpos := Pos('.', S);
> If dotpos = 0 Then
> Result := Result + S
> Else Begin
> Result := Result + RemoveLeadingZeros( Copy(S, 1,
dotpos-1))
> + '.';
> Delete(S, 1, dotpos);
> End;
> Until dotpos = 0;
> End;
>

With a little help from HyperString.<g>

procedure CondenseIPaddress(var S:AnsiString);
var
I:Integer;
begin
I := ScanFF(S,'00',1);
while I>0 do begin
if (I=1) or (S[I-1] = '.') then Delete(S,I,1) else
Inc(I,2);
I := ScanFF(S,'00',I);
end;
end;

--
Ernie Deel, EFD Systems
-----------------------------------------------
Any sufficiently advanced technology
is indistinguishable from a rigged demo.


Leicester Brad Ford Jr.

unread,
Mar 10, 1999, 3:00:00 AM3/10/99
to
"Philippe Ranger" <.> wrote:

>Wazis: >>
>How can I remove leading zeros of a string?

Just wanted to throw my "hat" in...

>Function unZero (s: string): string;
>(*Returns s with zeros removed iff they both come at the beginning or after
> a non-digit, and precede a digit (0 included).*)
>Var
> j: integer;
> nDel: integer; (*number of chars to delete in current run of 0s
> -1 means we cannot delete the first 0 we meet*)
>Begin
> nDel := -1;
> for j := length(s) downto 1 do case s[j] of
> '0': inc(nDel);
> '1'..'9': nDel := 0;
> else begin
> if (nDel > 0) then delete(s, j+1, nDel);
> nDel := -1;
> end;
> end;
> if (nDel > 0) then delete(s, 1, nDel);
> result := s;
>End;
>

Wanted to try something easier to read... VAL and STRTOINT does
basically what you want (i.e. remove "0"s).

// *Assumption* no non numeric characters besides the "."

Function unZero (s: string): string; // works in TP
var i, j, p: integer; t, Result: string;
begin
s := s + '.'; Result := '';
repeat
p := pos('.', s);
if p = 0 then BREAK;
val(copy(s, 1, pred(p)), i, j);
str(i, t);
Result := Result + t + '.';
s := copy(s, succ(p), length(s));
until false;
Result := copy(Result, 1, length(Result) - 1);
unZero := Result;
end;

// Don't have DELPHI running now, so can't check for errors...
// But I believe the principle to be sound.

Function unZero (s: string): string; // works in DELPHI
var i, p: integer; t: string;
begin
s := s + '.'; Result := '';
repeat
p := pos('.', s);
if p = 0 then BREAK;
t := copy(s, 1, pred(p));
Result := Result + IntToStr(StrToInt(t)) + '.';
s := copy(s, succ(p), length(s));
until false;
Result := copy(Result, 1, length(Result) - 1);
end;


Thanks,
--------------------------------------------
Remove the "X" in the email address to reply

Philippe Ranger

unread,
Mar 10, 1999, 3:00:00 AM3/10/99
to
Ernie: >>With a little help from HyperString
<<

Pretty neat!

PhR

Philippe Ranger

unread,
Mar 10, 1999, 3:00:00 AM3/10/99
to
Brad: >>

Result := Result + IntToStr(StrToInt(t)) + '.';
<<

Nice. It translates the implicit specs more directly than does my code.

PhR

Philippe Ranger

unread,
Mar 10, 1999, 3:00:00 AM3/10/99
to
Brad: >>
Thank you, how was your vacation (was it a vacation?)
<<

Yes, mostly for my girlfriend. Skiing vacation with mucho rain...

PhR

Wasis Sugiono

unread,
Mar 11, 1999, 3:00:00 AM3/11/99
to
How about this:

IP := MaskEdit1.Text
MaskEdit1.EditMask := '099\.099\.099\.099;1;_'
So there must be 3 dots and at least a numeric in each gap

Function TProperties.IsValidIP(IP : String) : Boolean;
Var
i, j : Integer;
Addr : Array[1..4] of Integer;
Begin
IP := Trim(IP) + '.';
{Remove leading zeros}
j := 0;
Repeat
Inc(j);
i := Pos('.', IP);
Try
Addr[j] := StrToInt(Copy(IP, 1, i-1));
Result := Addr[j] in [0..255]
Except
Result := False;
End;
Delete(IP, 1, i);
Until (j > 3) OR (not Result);
If Result Then
ServerIP := Format('%d.%d.%d.%d', [Addr[1], Addr[2], Addr[3],
Addr[4]]);
End;

Leicester Brad Ford Jr.

unread,
Mar 11, 1999, 3:00:00 AM3/11/99
to
"Philippe Ranger" <.> wrote:

Thank you, how was your vacation (was it a vacation?)

Myself, I am taking my wife (and daughter) to Las Vegas for a couple
of days to relax. I don't gamble but its good to get away from the
phone once in a while.

See ya,

David Arnall

unread,
Mar 11, 1999, 3:00:00 AM3/11/99
to
Sorry, but this doesn't seam to work for me.

CondenseIPaddress(011.100.001.011)
gives 011.100.001.011

D3 and HyperString v4.0

David


Ernie Deel wrote in message <7c68d1$jc...@forums.borland.com>...

Rick Rogers (TeamB)

unread,
Mar 11, 1999, 3:00:00 AM3/11/99
to
On Wed, 10 Mar 1999 22:06:10 -0500, "Philippe Ranger" <.> wrote:

> mostly for my girlfriend. Skiing vacation with mucho rain...

...an opportunity to sip hot cocoa in front of a blazing fire.

--
Rick Rogers (TeamB) | Fenestra Technologies
http://www.fenestra.com/

Ernie Deel

unread,
Mar 11, 1999, 3:00:00 AM3/11/99
to
David Arnall wrote in message
<7c7g0u$k7...@forums.borland.com>...

>Sorry, but this doesn't seam to work for me.
>

Check your implementation. The input string parameter must be
declared as "var".

Also, as given it only eliminates duplicate leading zeros. To
eliminate *all* leading zeros is similarly easy.

procedure CondenseIPaddress(var S:AnsiString);
var
I:Integer;
begin

I := 1;
while ScanW(S,'0#',I)>0 do


if (I=1) or (S[I-1] = '.') then Delete(S,I,1)
else Inc(I,2);

end;

Given S := '011.100.001.011' this function returns
'11.100.1.11' on my machine.

Leicester Brad Ford Jr.

unread,
Mar 14, 1999, 3:00:00 AM3/14/99
to
ri...@fenestra.com (Rick Rogers (TeamB)) wrote:

>On Wed, 10 Mar 1999 22:06:10 -0500, "Philippe Ranger" <.> wrote:
>
>> mostly for my girlfriend. Skiing vacation with mucho rain...
>
>...an opportunity to sip hot cocoa in front of a blazing fire.

Now that sounds like a vacation.

0 new messages