How to split string without for loop in batch file

前端 未结 6 926
暗喜
暗喜 2020-12-06 05:33

I want to split a string in two parts, without using any for loop.

For example, I have the string in a variable:

str=45:abc

I want

6条回答
  •  独厮守ぢ
    2020-12-06 06:22

    In the Light of people posting all sorts of methots for splitting variables here i might as well post my own method, allowing for not only one but several splits out of a variable, indicated by the same symbol, which is not possible with the REM-Method (which i used for some time, thanks @jeb).

    With the method below, the string defined in the second line is split into three parts:

    setlocal EnableDelayedExpansion
    set fulline=one/two/three or/more
    set fulline=%fulline%//
    REM above line prevents unexpected results when input string has less than two /
    
    set line2=%fulline:*/=%
    set line3=%line2:*/=%
    
    set line1=!fulline:/%line2%=!
    set line2=!line2:/%line3%=!
    
    setlocal DisableDelayedExpansion
    
    echo."%line1%"
    echo."%line2%"
    echo."%line3%"
    

    OUTPUT:

    "one"
    "two"
    "three or/more//"
    

    i recommend using the last so-created partition of the string as a "bin" for the remaining "safety" split-characters.

提交回复
热议问题