Solved

How to trim whitespace from a string?

Posted on 2008-10-15
8
19,632 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Log files are useful in diagnosing and repairing problems.  This is a list of common log files and their standard locations that I've compiled.   While this is not exhaustive, it is a pretty good list that I've found to be useful.  I may update it f…
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…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now