Forums before death by AOL, social media and spammers... "We can't have nice things"
|    alt.msdos.batch.nt    |    Fun with Windows NT batch files    |    68,980 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 67,101 of 68,980    |
|    Sponge Belly to All    |
|    split string into single characters    |
|    30 Dec 17 08:24:17    |
   
   From: sponge.belly@mailinator.com   
      
   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   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca