Dos - String Manipulation

By xngo on February 28, 2019

String Substitution

SET Str="Hello, World"
ECHO %Str%
REM Replace all 'o' with 'X'
SET Str=%Str:o=X%
ECHO %Str%

Substring

REM Get string starting from the 5th position to the end.
SET mySubstring=This String will be cut.
ECHO %mySubstring%
SET mySubstring=%mySubstring:~5%
ECHO %mySubstring%

REM =================== OR ======================

REM Use CALL and SET commands to extract a substring with variable length.
SET start=5
SET length=12
SET string=This long string will be cutted.
CALL SET substring=%%string:~%start%,%length%%%
ECHO %substring%

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.