Batch Script – Mid String
In this article , we are going to learn how to use the concept of Mid String using Batch Script.
Using the concept of ‘Mid String’ we are extracting out a sub string between two indices of any given string.
Batch Script :
@echo off
set str=GeeksforGeeks
echo %str%
set str=%str:~5,-5%
echo %str%
pause
Using above code we are going to print a substring from 5 to -5 index from given string(i.e. GeeksforGeeks).
Explanation :
- By using ‘ set ‘ we are getting input of any string.
set str=GeeksforGeeks
- In the next line using ‘ echo %str% ‘ we are printing our input string.
- Now in the next line using indexing we are going to eliminate characters from both ends.
- General Representation : set str=%string:~FROM, TO% .
set str=%str:~5,-5%
- In the above representation we have to provide the index from which we want our new string till its ending index.
- In above code we have ‘5, -5’ , so from index 5 which is ‘s’ till index -5( negative indexing) which is ‘G’.
- So the substring between these two indexes will be printed as output.
- Then we are using ‘pause’ , to hold the screen until any key is pressed , so that we can read our output.
As we can clearly see that characters between ‘5’ and ‘-5’ is printed as output ,so using the following code we can use the concept of Mid String.
Last Updated :
04 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...