Quickest way to remove trailing zeros from a string?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
CirothUngol
Posts: 46
Joined: 13 Sep 2017 18:37

Quickest way to remove trailing zeros from a string?

#1 Post by CirothUngol » 15 Sep 2017 10:36

First a quick howdy and shout out the all the great contributors at Dostips! I've been writing batch stuff since the 1980s, how could I possibly have not known about this site? Plenty of awesome stuff up here, and I'm sure to have questions about most of it. But first, to my subject at hand:

I need to remove both leading and trailing zeros from a number string. The leading zeros are quick and easy.

Code: Select all

FOR /F "tokens=* delims=0" %%A IN ("%numStr%") DO SET "answer=%%A"
Simple and elegant. Not so much with the trailing zeros... I'm currently using a horrid GOTO construct:

Code: Select all

:label
IF "%numStr:~-1%" EQU "0" SET "numStr=%numStr:~0,-1%" & GOTO :label
EXIT /B 0
...which is both ugly, and slower than Christmas. I'm hoping but there may be a quick and elegant answer that I'm overlooking. Any ideas?


aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Quickest way to remove trailing zeros from a string?

#3 Post by aGerman » 15 Sep 2017 11:30

Yes the same thread I had in mind.

Code: Select all

@echo off &setlocal

set "numStr=00000000000000000102400000000000000000000000000000000000000000000000000000000000000000000"

setlocal EnableDelayedExpansion
set /a "zeros=0, n=4096"
for /l %%i in (1 1 12) do set "zeros=!zeros!!zeros!"
for /l %%i in (1 1 13) do  if defined numStr for %%j in (!n!) do (
  if "!numStr:~-%%j!"=="!zeros:~-%%j!" set "numStr=!numStr:~0,-%%j!"
  if "!numStr:~0,%%j!"=="!zeros:~-%%j!" set "numStr=!numStr:~%%j!"
  set /a "n/=2"
)
echo !numStr!
pause

Steffen

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Quickest way to remove trailing zeros from a string?

#4 Post by Squashman » 15 Sep 2017 12:47

aGerman wrote:Yes the same thread I had in mind.

I think they just want a right trim of the zero. I get 1024 as the final output string instead of 000000000000000001024

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Quickest way to remove trailing zeros from a string?

#5 Post by penpen » 15 Sep 2017 12:56

This might help you:

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
set "numStr=00012345678900000000"
for %%a in ("%numStr:0= %") do set "answer=%%~na"
set "answer=%answer: =0%"

set answer
goto :eof


penpen

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Quickest way to remove trailing zeros from a string?

#6 Post by aGerman » 15 Sep 2017 13:08

Squashman wrote:I think they just want a right trim of the zero.
Hmmm...
CirothUngol wrote:I need to remove both leading and trailing zeros from a number string. The leading zeros are quick and easy.

Of course I could have used "tokens=*" first to avoid the additional string manipulations.

Code: Select all

@echo off &setlocal

set "numStr=00000000000000000102400000000000000000000000000000000000000000000000000000000000000000000"

setlocal EnableDelayedExpansion
set /a "zeros=0, n=4096"
for /l %%i in (1 1 12) do set "zeros=!zeros!!zeros!"
for /f "tokens=* delims=0" %%i in ("!numStr!") do set "numStr=%%i"
for /l %%i in (1 1 13) do if defined numStr for %%j in (!n!) do (
  if "!numStr:~-%%j!"=="!zeros:~-%%j!" set "numStr=!numStr:~0,-%%j!"
  set /a "n/=2"
)
echo !numStr!
pause

Although in combination with penpen's approach it's getting much nicer.

Code: Select all

@echo off &setlocal

set "numStr=00000000000000000102400000000000000000000000000000000000000000000000000000000000000000000"
for /f "tokens=* delims=0" %%i in ("%numStr%") do set "numStr=%%i"
for %%a in ("%numStr:0= %") do set "numStr=%%~na"
set "numStr=%numStr: =0%"

echo "%numStr%"
pause


Steffen

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Quickest way to remove trailing zeros from a string?

#7 Post by Squashman » 15 Sep 2017 13:15

I stand corrected. :mrgreen:

CirothUngol
Posts: 46
Joined: 13 Sep 2017 18:37

Re: Quickest way to remove trailing zeros from a string?

#8 Post by CirothUngol » 15 Sep 2017 13:36

Yep, that seems to be exactly what I'm looking for. Using the same type of binary chop down as the strLen function, clever... I probably never would have thought of it, or found it.

At the risk of asking this question for the umpteenth time, is there an updated Library of functions and their macro equivalents? All of this Brilliance really should be collected into one spot so that interested parties all over the world would find it easy to discover and peruse.

@squashman: I actually want to do both of them, but at different times. The FOR delims= is so quick and simple that I will probably continue to use it for the leading zeros and resort to the second function for the trailing zeros.

@penpen: again, very clever. Having read dozens of your posts in the last week, I would have expected no less. Thanks for your example that illustrates both speed and efficiency. I was originally using the FOR /F construct to separate a decimal number into integer and fraction portions (%%~nA is integer, %%~xA is fraction) but discovered that it would stop working at around 490 digits, returning a null for both name and extension portions of the input, so I'm afraid that feeding the input directly into a FOR Loop might not be the way to go. I'll do a couple of tests tonight when I get off of work.

@aGerman: and a final solution emerges. As previously stated, I will split this into two parts so that I can do leading and trailing separately, but if penpen's suggestion works with really large numbers I will use it for the trailing because it appears to be the shortest, simplest, and quickest solution.

I have to openly admit, the guys on this forum are awesome! I came back after a few hours hoping to have even one reply, instead I Got 5... and a solution! This kind of attention can really spoil a guy, so I'm sure to be back for more. ^_^

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Quickest way to remove trailing zeros from a string?

#9 Post by penpen » 15 Sep 2017 13:41

I shortened the solution by one line:

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
set "numStr=00000000000000000102400000000000000000000000000000000000000000000000000000000000000000000"

for %%a in ("%numStr:0= %") do set "answer=%%~na"
for /f "tokens=* delims=0" %%a in ("%answer: =0%") do set "answer=%%a"

set answer

goto :eof


penpen

catalinnc
Posts: 39
Joined: 12 Jan 2015 11:56

Re: Quickest way to remove trailing zeros from a string?

#10 Post by catalinnc » 15 Sep 2017 13:57

very nice...
_

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Quickest way to remove trailing zeros from a string?

#11 Post by penpen » 15 Sep 2017 14:05

CirothUngol wrote:very clever. Having read dozens of your posts in the last week, I would have expected no less.
Thanks, but the i haven't found this method. I'm unsure where i read it:
I think it was in one of the topics here on dostips.
(Edit:) Samir has used this trick first, see: http://www.dostips.com/forum/viewtopic.php?p=27394#p27394.

CirothUngol wrote:but discovered that it would stop working at around 490 digits
Interesting find!
I didn't test my solution on that large strings, but now i found out:
Under my Windows 10 (32 bit) the above for-related solutions all only work up to 498 characters; one more and you only get a multiple not usefull characters (U+3F3F).

So you might chop the string into 498 bytes parts (or smaller if 498 is the maximum on your system).


penpen

Edit: Added link, where i first read about the trick i used above.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Quickest way to remove trailing zeros from a string?

#12 Post by aGerman » 15 Sep 2017 14:25

CirothUngol wrote:is there an updated Library of functions and their macro equivalents?
Unfortunately not yet. A year ago I began to collect some links to those threads but I discontinued due to a lack of time and I wasn't quite uncertain how to cluster them. As you might have discovered there are some solutions based on hybrid scripts where Batch is combined with other scripting languages (like JScript) or programming languages (like C#). Furthermore there are some 3rd party utilities uploaded at DosTips. Should I simply merge them with pure Batch solutions or should I rather leave them out? I'm still struggling...

(If penpen's solution doesn't work for long strings...) You can pre-define the zeros variable in case you need it several times in your script. That way you may improve the performance a little.

Steffen

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Quickest way to remove trailing zeros from a string?

#13 Post by Squashman » 15 Sep 2017 14:44

For some reason, this thread made me think about this old question.
viewtopic.php?f=3&t=3362

CirothUngol
Posts: 46
Joined: 13 Sep 2017 18:37

Re: Quickest way to remove trailing zeros from a string?

#14 Post by CirothUngol » 15 Sep 2017 14:54

I'll probably just use the trailing portion of the original function so that there are no limitations on the size of the string. Thanks for the suggestions guys!

As to the categories for a potential Library, I think it would be most prudent to separate functions into ones that do or do not require third party software or external scripts. A lot of people don't like to use 'helper apps', and plenty of people can't because of security or network restrictions.

As an outsider who has just discovered this site and is very interested in discovering its contents, I would have to admit that it's rather daunting. It would seem that there is only one area in the Forum, which means every search turns up hundreds and hundreds of results. I've spent the last four or five evenings reading up on batch macros and other fun Advanced Techniques, but searching for and finding the information to read took about half of that time. In fact, I'm still unsure where the macros development is at presently and if there are any newer or better developments since the threads I read that were posted about three or four years ago.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Quickest way to remove trailing zeros from a string?

#15 Post by penpen » 15 Sep 2017 17:08

Another option, that should work on any (non-empty) environment variable containing a number string:

Code: Select all

@echo off
setlocal enableExtensions enableDelayedExpansion
set "numStr=000000000000000001024000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"

set "r="
set "l=0"
for /f %%a in ('cmd /u /v:on/c"echo(^!numStr^!" ^| find /v ""') do (
   if defined r (
      if "%%~a" == "0" ( set /a "r+=1"
      ) else             set    "r=0"
   ) else (
      if "%%~a" == "0" ( set /a "l+=1"
      ) else             set    "r=0"
   )
)
set "answer=!numStr:~%l%,-%r%!"

set answer
goto :eof
Note: I haven't done any speed test.


penpen

Post Reply