Solved

How to trim whitespace from a string?

Posted on 2008-10-15
8
19,727 Views
Last Modified: 2012-05-05
Hello,

I'm trying to trim whitespace from a string variable.  I've figured out how to left trim, but can't figure out how to right trim.  There are some examples on the web, but to work they make assumptions like the maximum length of string, or that it won't contain 2 # symbols together.  I can't make this assumption so would need it to be fail safe.

In short, I need a solution to trim spaces from the right end of the string.

My code is shown below, with the left trim working.

Many thanks,
Steve
--

:trim
 
rem --- Set variable and remove any surrounding quotes
set _string=%~1
 
rem --- Remove whitespace at left
for /F "tokens=*" %%s in ("%_string%") do set _trimmed=%%s
 
rem --- Remove whitespace at right
 
 
echo [%_trimmed%]
goto :eof

Open in new window

0
Comment
Question by:sda100
  • 7
8 Comments
 
LVL 43

Expert Comment

by:Steve Knight
ID: 22720311
Just use this concept:

set _trimmed=%_string: =%

where it replaces space with nothing (i.e. removes)

e.g.

set datewithdashes=%date:/=-%

hth

Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 22720354
Ignore that, misread question!
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 22720400
if you can use a VBS you could get that to do it:

REM trim.vbs
wscript.echo trim(wscript.arguments(0))

@echo off
set _string=%~1
for /f "tokens=*" %%a in ('cscript //nologo trim.vbs "%_String%"') do set _trimmed=%%~a
echo %_trimmed%

but can't think of easy batch way without a lot of fiddling

Steve
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 43

Expert Comment

by:Steve Knight
ID: 22720418
Ahh actually thinking about it again...

@echo off
set _string=%~1
:loop
 if not "%_string:~-1%"==" goto :next
 set _string=%_string:0,-1%
 goto loop
:next
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 250 total points
ID: 22720485
Not doing well today, that was previous attempt:

@echo off
set _string=%~1
set _trimmed=%_string%
:loop
 if not "%_trimmed:~-1%"==" " goto next
 set _trimmed=%_trimmed:~0,-1%
 goto loop
:next
echo %_trimmed%X
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 22720508
Add this in as an alternative to the ltrim too if you wish:

:loop2
 if not "%_trimmed:~0,1%"==" " goto next2
 set _trimmed=%_trimmed:~1%
 goto loop2
:next2

Steve
0
 
LVL 9

Author Closing Comment

by:sda100
ID: 31506274
Thanks Steve,

I actually came up with the exact same solution before I came back to this question, but it did take me 2 hours - not 2 minutes!

Steve :)
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 22721181
haha, no problem at least you got yourself a solution.

Steve
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My purpose is to describe the basic concepts of virtual memory as implemented in a modern Windows-based operating system. I will also describe the problems inherent in older systems and how virtual memory solves them. The dark ages - before virtu…
When you upgrade from Windows 8 to 8.1 or to Windows 10 or if you are like me you are on the Insider Program you may find yourself with many 450MB recovery partitions.  With a traditional disk that may not be a problem but with relatively smaller SS…
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question