0% found this document useful (0 votes)
9 views

String Processing

Uploaded by

yard.pro.991
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

String Processing

Uploaded by

yard.pro.991
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

8/30/2019 Windows Batch Scripting - Wikibooks, open books for an open world

String processing[ edit ]

Getting a substring of a non-empty variable:

set a=abcdefgh
echo %a:~0,1% & rem from index 0, length 1; result: a
echo %a:~1,1% & rem from index 1, length 1; result: b
echo %a:~0,2% & rem from index 0, length 2; result: ab
echo %a:~1,2% & rem from index 1, length 2; result: bc
echo %a:~1% & rem from index 1 to the end; result: bcdefgh
echo %a:~-1% & rem from index -1 (last char) to the end; result: h
echo %a:~-2% & rem from index -2 (next-to-last) to the end; result: gh
echo %a:~0,-2% & rem from index 0 to index -2, excl.; result: abcdef
echo %a:~0,-1% & rem from index 0 to index -1, excl.; result: abcdefg
echo %a:~1,-1% & rem from index 1 to index -1, excl.; result: bcdefg

Testing substring containment:

if not "%a:bc=%"=="%a%" echo yes


If variable a contains "bc" as a substring, echo "yes".
This test is a trick that uses string replacement, discussed below.
This test does not work if the variable contains a quotation mark.

Testing for "starts with":

if %a:~0,1%==a echo yes & rem If variable a starts with "a", echo "yes".
if %a:~0,2%==ab echo yes & rem If variable a starts with "ab", echo "yes".

String replacement:

set a=abcd & echo %a:c=% & rem replace c with nothing; result: abd
set a=abcd & echo %a:c=e% & rem replace c with e; result: abed;
set a=abcd & echo %a:*c=% & rem replace all up to c with nothing; result: d
rem Above, the asterisk (*) only works at the beginning of the sought pattern.

See also the help for SET command: set /?.

Splitting a string by any of " ", ",", and ";": ["space", "comma" and "semicolon":]

set myvar=a b,c;d


for %%a in (%myvar%) do echo %%a

file:///C:/Users/Rocky/Taskbar Links/Windows Batch Scripting - Wikibooks.html 1/2


8/30/2019 Windows Batch Scripting - Wikibooks, open books for an open world

Splitting a string by semicolon, assuming the string contains no quotation


marks:

@echo off
set myvar=a b;c;d
set strippedvar=%myvar%
:repeat
for /f "delims=;" %%a in ("%strippedvar%") do echo %%a
set prestrippedvar=%strippedvar%
set strippedvar=%strippedvar:*;=%
if not "%prestrippedvar:;=%"=="%prestrippedvar%" goto :repeat

Limitations:

The above string processing does not work with parameter variables (%1,
%2, ...).

Links:

Variables: extract part of a variable (substring) at ss64


Variable Edit/Replace at ss64

file:///C:/Users/Rocky/Taskbar Links/Windows Batch Scripting - Wikibooks.html 2/2

You might also like