Open In App

Batch Script – Remove All Spaces

Last Updated : 28 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to see how to remove all spaces from any string using Batch String.

Example :

Input: G e e k s     f o r    G e e k s
Output: GeeksforGeeks

Approach :

  • By using ‘ set ‘ we are getting input of any string.
    • Example: set str=input string
  • In the next line using ‘ echo %str% ‘ we are printing our input string.
  • In the next line of code, we are going to remove all spaces in the input string.
  • Using ‘ := ‘ operator we are removing spaces in any string.
  • We have to type any character between and = to remove that character from any given input string.
    • For example: str=%str:e=% (this will remove e from the input string)
  • So for removing spaces we will use ‘ : = ‘ .
  • ‘ pause ‘ is used to hold the screen until any key is pressed.

Code:

@echo off
set str=G e e k s     f o r    G e e k s
echo %str%
set str=%str: =%
echo %str%
pause

Output:

batch script to remove all spaces.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads