Open In App

Batch Script – Right String

In this article, we are going to learn how to use the concept of Right String using Batch Script.

Right String uses the concept of negative indexing. We have to extract the characters using :~ followed by the negative index of the character from which we want to print a substring from the main string. Using the right string we can print characters from the right side of any given string.



Batch Script :

@echo off 
set str=Geeks for Geeks
echo %str% 

set str=%str:~-5% 
echo %str%

pause

Using the above code we are going to print a substring from -5 to -1 index from the given string(i.e. Geeks for Geeks).

Explanation:



Another Approach :

Batch Script :

@echo off 
set str=GFG is best platform for Geeks
echo %str% 

:: 10 characters from right will be printed
set str=%str:~-10%  
echo %str%

pause

In this example, we have given the index value as ‘-10’ which means a substring having 10 characters from the right side will be extracted and printed.

Output:

As we can clearly see that a substring of 10 characters from the right is printed as output.

Article Tags :