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

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

#16 Post by CirothUngol » 15 Sep 2017 21:24

Just stopped to post the code being used...

Code: Select all

:math_trailing numVar -- removes trailing zeros
SET m_t=4096
FOR /L %%X in (1,1,13) DO IF DEFINED %~1 FOR %%Y IN (!m_t!) DO (
   IF "!%~1:~-%%Y!"=="!zeros:~-%%Y!" SET "%~1=!%~1:~0,-%%Y!"
   SET /A m_t/=2
)
IF DEFINED %~1 IF "!%~1:~-1!" EQU "." SET "%~1=!%~1:~0,-1!"
EXIT /B 0
Small, simple, full-featured and works like a charm. Thanks again guys, you'll be the first to know next time I hit a wall. ^_^

Aacini
Expert
Posts: 1631
Joined: 06 Dec 2011 22:15
Location: México City, México

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

#17 Post by Aacini » 16 Sep 2017 10:50

I think that this method should run faster (and it is much simpler):

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set "numStr=000000000000000001002300045600000000000"

for /F "tokens=* delims=0" %%a in ("%numStr%") do set "answer=%%a"
set "l="
set "r="
set "answer=%answer:0=" & (if defined r set "answer=!answer!!l!!r!" & set "l=") & set "l=!l!0" & set "r=%"
if defined r set "answer=!answer!!l!!r!"

echo Answer: %answer%

Antonio

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

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

#18 Post by aGerman » 16 Sep 2017 11:20

Never thought about using this technique for that task.
Great idea, Antonio!

Steffen

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

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

#19 Post by CirothUngol » 16 Sep 2017 11:44

Aacini wrote:I think that this method should run faster (and it is much simpler)
Faster, I have no doubt. Clever and elegant, sure. Simpler... don't know about all that. I certainly see how it works, but definately falls into the "I'd have never thought of that" category. Love it!

My issue with it is max string size. The replacement adds dozens of characters, so it gives out way too quick. Awesome technique, though! I'm adding that page to the dozen+ Dostips tabs that I have open right now. Looks like another fun Saturday spent learning about WinNT batch. ^_^

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

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

#20 Post by penpen » 16 Sep 2017 12:07

I think this is the fastest code that also works on full environment variables:

Code: Select all

@echo off
setlocal enableExtensions enableDelayedExpansion

set "numStr=000000000000000001024000000000000000000000000000000000000000000000"

for /F "tokens=* delims=0" %%a in ("!numStr!") do set "answer=%%a"
set /a "r=range=0"
if "!numStr:~-1!" == "0" (
   for %%a in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
      set /a "r+=%%~a"
      for %%b in ("!r!") do for /f "delims=0" %%c in ("!answer:~-%%~b!") do set /a "r-=%%~a"
   )
   set "range=0,-!r!"
)
set "answer=!answer:~%range%!"

set answer

goto :eof


penpen

Post Reply