Solved

How to trim whitespace from a string?

Posted on 2008-10-15
8
20,243 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

This article covers how to install the Microsoft Windows Operating System (OS). What is covered in this article:  > Different Versions and Editions of the Windows OS  > Upgrading versus Fresh Installation of the OS           - Steps to take pr…
Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
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…

695 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