1

Supposing you have a string value in a variable STRING, how can you remove leading and trailing whitespaces with a single command (line)?

For instance, the string is (the _ stands for tab here; double-quotes just for illustration, not part of string value):

"  two spaces, trimmed text string, space-tab-space _ "

The expected output is (double-quotes again not part of string):

"two spaces, trimmed text string, space-tab-space"

How to accomplish that?

In this context, I'm talking about spaces and (horizontal) tabulators when I say "whitespaces".

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • It's not possible with a single batch command, but see [How to remove trailing and leading whitespace for user-provided input in a batch file?](http://stackoverflow.com/q/3001999) – DavidPostill Aug 15 '15 at 14:32

2 Answers2

3

It is not possible to trim both spaces and tabs in a single line, but just for spaces you may use this trick:

@echo off
setlocal EnableDelayedExpansion

set "x=  two spaces, trimmed text string, space-space-space   "
echo "%x%"

rem The single line:
set "word=%x: =" & (if "!word!" neq "" set "x2=!x2! !word!") & set "word=%" & set "x2=!x2:~1!"

echo "%x2%"
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Interesting. Works even with `set "x= spaces, ..., space(s)-tab "` (note a trailing `` after spaces). However, I'm afraid it will fail for `set "x= spaces, ..., no-trailing-space"` – JosefZ Aug 15 '15 at 16:33
  • @JosefZ: Yes. The [original code](http://www.dostips.com/forum/viewtopic.php?f=3&t=6429&p=41356#p41356) have an additional `set "x=%x% "` line to avoid this problem, but I cheat a little in order to make this method fit "in one line". **`;-)`** – Aacini Aug 15 '15 at 16:53
  • Excellent effort, @Aacini, thank you! It would be cool if you mention the required trailing space in your answer somehow (perhaps a `rem`). Furthermore, I found out that some characters cause trouble: `%` signs (must be doubled prior to processing), `!` marks (they simply disappear; also `^!` and `^^!` sequences), and double-quotes `"`; it would be great too if you mentioned these restrictions in your answer. – aschipfl Aug 17 '15 at 18:20
  • I just found out that two or more consecutive spaces are changed to a single one by this command line... – aschipfl Jul 26 '16 at 17:25
  • I just found a very nice way to trim strings: take a look at [this post](http://stackoverflow.com/a/38598214)... – aschipfl Jul 26 '16 at 19:31
  • 1
    @aschipfl: Two posts after the one that contains [the original code](http://www.dostips.com/forum/viewtopic.php?f=3&t=6429&p=41356#p41356) (this is the third time I included such link here) I posted a modification of this method with this explanation: _"I realized that my previous method to trim leading and trailing spaces also reduce several spaces between words to just one. The new method below correctly preserve multiple spaces between words"_. – Aacini Jul 26 '16 at 22:04
0

Concerning leading whitespaces only, the following code works (_ means TAB):

set "STRING=  two spaces, trimmed text string, space-tab-space _ "

rem no "delims=" option given, so defaulting to spaces and tabs:
for /F "tokens=* eol= " %%S in ("%STRING%") do set "LEFTTRIMMED=%%S"

echo "%LEFTTRIMMED%"

However, the trailing whitespaces are still present; but they can removed by the approaches shown in this thread.

aschipfl
  • 33,626
  • 12
  • 54
  • 99