Skip to content

Batch Script String Manipulation

How to manipulate strings in Batch Script using substring extraction, concatenation, replacement, and other operations with syntax examples like %variable:~start,length% and string interpolation.

Batch Script String Manipulation

In Batch Script, a string is an ordered collection of characters.

Batch scripts have the following commands which are used to carry out string manipulation in strings:

%variable:~num_chars_to_skip%
%variable:~num_chars_to_skip,num_chars_to_keep%

and this can include negative numbers:

%variable:~num_chars_to_skip, -num_chars_to_keep%
%variable:~-num_chars_to_skip,num_chars_to_keep%
%variable:~-num_chars_to_skip,-num_chars_to_keep%

The most common operations on strings in Batch Scripting are explained below:

Create a String

A string can be created using the set command in the following way:

Example of String creationbatch
@echo off 
:: This program displays Hello World 
set message=Hello World 
echo %message%
Outputplaintext
Hello World

Empty String

An empty string can be created by assigning no value to it during initialization:

Empty string initializationbatch
set variable_name=

To check for an empty string, you need to enclose the variable name in square brackets and compare it with a value in square brackets:

Empty String Checkbatch
if [%variable_name%]==[] echo "True"

Below is a complete example with empty strings:

Example of Empty Stringbatch
@echo off 
set a= 
set b=Hello 
if [%a%]==[] echo "String A is empty" 
if [%b%]==[] echo "String B is empty "
Outputplaintext
String A is empty

String Interpolation

String interpolation is a way to construct a new String value from a mix of constants, variables, literals, and expressions by including their values inside a string literal.

In Batch Scripting, the string interpolation can be done using the set command and lining up the numeric defined variables or any other literals in one line when using the set command.

Example of String Interpolationbatch
@echo off
set a=Hello
set b=World
set /A d=123
set c=%a% and %b% %d%
echo %c%
Outputplaintext
Hello and World 123

String Concatenation

You can use the set operator to concatenate two strings or a string and a character, or two characters

Example of String Concatenationbatch
@echo off 
SET a=Hello 
SET b=World 
SET c=The %a% and the %b% 
echo %c%
Outputplaintext
The Hello and the World

String Length

In Batch Scripting there is no length function defined to find the length of a string.

But there are custom-defined functions which can be used for the same.

For example, you can reuse what is defined below:

Example of String Lengthbatch
@echo off
set str=Hello World
call :strLen str strlen
echo String is %strlen% characters long
exit /b

:strLen
setlocal enabledelayedexpansion

:strLen_Loop
   if not "!%1:~%len%!"=="" set /A len+=1 & goto :strLen_Loop
(endlocal & set %2=%len%)
goto :eof
Outputplaintext
11

String to Integer

A variable that has been set as a string using the set variable can be converted to an integer using the /A flag using the set variable.

Example of String to Integerbatch
@echo off
set var=12345
set /A var=%var% + 5
echo %var%
Outputplaintext
12350

Align Right

It is used to align text to the right, normally to improve readability of columns of numbers.

In the following example we add leading spaces to a string to make sure the output lines up: we want for variables no longer than 8 characters to add 8 spaces at the beginning and then show only the last 8 characters of the variable.

Example of Align Rightbatch
@echo off
set x=3000  
set y=2  
set x=%x%  
set y=%y%  
echo.X=%x:~-8%  
echo.Y=%y:~-8%
Outputplaintext
X=    3000
Y=       2

Substring starting at the beginning of the string (Left string)

This is used to extract characters from the beginning of a string.

The substring syntax is as follows:

set substring=%variable_name:~from_index,to_index%

An example:

Example of Left Stringbatch
set str=Helloworld
echo.%str%
set str=%str:~0,5%
echo.%str%
Outputplaintext
Helloworld
Hello

Substring (Mid string)

This is used to extract a substring via the position of the characters in the string.

The substring syntax is as follows:

set substring=%variable_name:~from_index,to_index%
Example of Mid String batch
@echo off 
set str=Helloworld 
echo %str%

set str=%str:~3,7% 
echo %str%
Outputplaintext
Helloworld
loworld

Substring starting at the end of the string (Right string)

This is used to extract characters from the end of a string.

The substring syntax is as follows:

set substring=%variable_name:~-index%
Example of Right Stringbatch
@echo off 
set str = This message needs changed. 
echo %str% 

set str = %str:~-8% 
echo %str%
Outputplaintext
This message needs changed.
changed.

Remove

The string substitution feature can also be used to remove a substring from another string.

The syntax is as follows:

set newstr=%variable_name:stringtobereplaced=newstring%

An example:

Example of Removebatch
@echo off 
set str=Batch scripts is easy. It is really easy. 
echo %str% 
set str=%str:is =% 
echo %str%
Outputplaintext
Batch scripts is easy. It is really easy.
Batch scripts easy. It really easy.

Remove Both Ends

This is used to remove the first and the last character of a string.

The syntax is as follows:

set substring=%variable_name:~from_index,to_index%

An example:

Example of Remove Both Endsbatch
@echo off 
set str=Batch scripts is easy. It is really easy 
echo %str% 

set str=%str:~1,-1% 
echo %str%
Outputplaintext
Batch scripts is easy. It is really easy
atch scripts is easy. It is really eas

Remove All Spaces

This is used to remove all spaces in a string by substitution.

The syntax is as follows:

set substring=%variable_name: =%

An example:

Example of Remove All Spacesbatch
@echo off 
set str=This string    has    a  lot  of spaces 
echo %str% 

set str=%str: =% 
echo %str%
Outputplaintext
This string has a lot of spaces
Thisstringhasalotofspaces

Replace a String

To replace a substring with another string use the string substitution feature.

The syntax is as follows:

set newstr=%variable_name:stringtobereplaced=newstring%
Example of Replace a Stringbatch
@echo off 
set str=This message needs changed. 
echo %str% 

set str=%str:needs=has% 
echo %str%
Outputplaintext
This message needs changed.
This message has changed.
Navigation