Solved

How to trim whitespace from a string?

Posted on 2008-10-15
8
19,526 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
Comment Utility
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
Comment Utility
Ignore that, misread question!
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 43

Accepted Solution

by:
Steve Knight earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
haha, no problem at least you got yourself a solution.

Steve
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
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…
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). …

771 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

10 Experts available now in Live!

Get 1:1 Help Now