Solved

How to trim whitespace from a string?

Posted on 2008-10-15
8
19,982 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
INTRODUCTION The purpose of this document is to demonstrate the Installation and configuration of the Data Protection Manager product. Note that this demonstration was prepared on the basis of Windows OS is 2008 R2 and DPM 2010. DATA PROTECTI…
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…

733 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