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:
@echo off
:: This program displays Hello World
set message=Hello World
echo %message%Hello WorldEmpty String
An empty string can be created by assigning no value to it during initialization:
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:
if [%variable_name%]==[] echo "True"Below is a complete example with empty strings:
@echo off
set a=
set b=Hello
if [%a%]==[] echo "String A is empty"
if [%b%]==[] echo "String B is empty "String A is emptyString 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.
@echo off
set a=Hello
set b=World
set /A d=123
set c=%a% and %b% %d%
echo %c%Hello and World 123String Concatenation
You can use the set operator to concatenate two strings or a string and a character, or two characters
@echo off
SET a=Hello
SET b=World
SET c=The %a% and the %b%
echo %c%The Hello and the WorldString 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:
@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 :eof11String 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.
@echo off
set var=12345
set /A var=%var% + 5
echo %var%12350Align 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.
@echo off
set x=3000
set y=2
set x=%x%
set y=%y%
echo.X=%x:~-8%
echo.Y=%y:~-8%X= 3000
Y= 2Substring 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:
set str=Helloworld
echo.%str%
set str=%str:~0,5%
echo.%str%Helloworld
HelloSubstring (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%@echo off
set str=Helloworld
echo %str%
set str=%str:~3,7%
echo %str%Helloworld
loworldSubstring 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%@echo off
set str = This message needs changed.
echo %str%
set str = %str:~-8%
echo %str%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:
@echo off
set str=Batch scripts is easy. It is really easy.
echo %str%
set str=%str:is =%
echo %str%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:
@echo off
set str=Batch scripts is easy. It is really easy
echo %str%
set str=%str:~1,-1%
echo %str%Batch scripts is easy. It is really easy
atch scripts is easy. It is really easRemove 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:
@echo off
set str=This string has a lot of spaces
echo %str%
set str=%str: =%
echo %str%This string has a lot of spaces
ThisstringhasalotofspacesReplace 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%@echo off
set str=This message needs changed.
echo %str%
set str=%str:needs=has%
echo %str%This message needs changed.
This message has changed.