Open In App

Batch Script - Remove Both Ends

Last Updated : 28 Nov, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we are going to learn how to remove characters from both ends of any given string using Batch Script.

Code :

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

set str=%str:~1,-1% 
echo %str%
pause
  • By using ' set ' we are getting input of any string
set str=input string
  • In the next line using ' echo %str% ' we are printing our string.
  • Now in the next line using indexing we are going to eliminate characters from both ends.
  • General representation - set str=%string:~start,end%
    • In the above representation '~start, end' will remove all characters after any given index.
  • For removing characters from both ends we are using start = 1 and end = -1, which is the index of the first and last character of any string.

Output :

Output: G and s is removed from input string.

As we can clearly see that characters from both ends are removed, so using the following code we can remove both ends from any given string.


Next Article
Article Tags :

Similar Reads