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

split string into single characters

22 views
Skip to first unread message

Sponge Belly

unread,
Dec 30, 2017, 11:24:19 AM12/30/17
to
Hello All!

I couldn't find an old thread on ambn about this, so I started a new
one. ;)

The :splitStr subroutine:

* accepts the name of a variable and prints out every character in
the string stored in that variable on a line by itself

* can be safely CALLed with delayed expansion disabled or enabled

* handles all ASCII characters (except NUL) including CR and LF

* doesn't use a dog-slow goto loop

* sets errorLevel with the length of the string

- - - -

@echo off & setLocal enableExtensions disableDelayedExpansion
(call;) %= sets errorLevel to 0 =%

set "testStr=uncopyrightable"
call :splitStr testStr
if errorLevel 1 (
>&2 echo(string is %errorLevel% char(s^) in length
) else (
>&2 echo(empty string
goto die
) %= if =%
goto end

:die
(call) %= sets errorLevel to 1 =%
:end
endLocal & goto :EOF

:splitStr string=
:: outputs string one character per line
setLocal disableDelayedExpansion
set "var=%1"

set "chrCount=0" & if defined var for /f "delims=" %%A in ('
cmd /v:on /q /c for /l %%I in (0 1 8190^) do ^
if "!%var%:~%%I,1!" neq "" (^
echo(:^!%var%:~%%I^,1^!^) else exit 0
') do (
set /a chrCount+=1
echo%%A
) %= for /f =%

endLocal & exit /b %chrCount%

- - - -

Enjoy and HNY!

- SB
0 new messages