To trim trailing spaces from a string

Dell / Optiplex gx270...
October 21, 2009 at 08:07:14
Specs: Windows XP, 2.992 GHz / 1021 MB
I have a string whic is made up of multiple words seperated by commas and has space in the trailling end also . I need to remove the spaces in the trailing end alone. example string is given below

examplestring = "I have spaces "


See More: To trim trailing spaces from a string

Report •


#1
October 21, 2009 at 09:00:46
@echo off > newfile & setLocal enableDELAYedexpansion

set str=I have spaces
> old echo !str!

:loop
if "!str:~-1!" equ " " (
set str=!str:~0,-1!
goto :loop
)

echo !str!> newfile


=====================================
Helping others achieve escape felicity

M2


Report •

#2
October 27, 2009 at 04:23:18
Thanks Mechanix ... this wrks perfectly fine :))

Report •

#3
October 29, 2009 at 11:24:12
Hey M2,

What needs to be done for trimmin trailing spaces from a file [with multiple strings on multiple lines]??


Report •

Related Solutions

#4
October 29, 2009 at 21:08:16
I see that you are trying to do this with a batch file which is cool, you could use a for loop and modify what is above so that it runs through and does what you want, but could I suggest a VBScript? It will trim the spaces off of the front and back of each line in your file and save it as the same file. All you have to do is change the first line to point toward the file you want to modify.


strFileName="C:\Your\File.txt"

Set objFS = CreateObject("Scripting.FileSystemObject")
StrContents = Split(objFS.OpenTextFile(strFileName).ReadAll, vbNewLine)
Set objTS = objFS.OpenTextFile(strFileName, 2)
For Each strLine In StrContents
objTS.WriteLine (Trim(strLine))
Next


Report •

#5
October 30, 2009 at 05:35:23
@echo off > newfile & setLocal enableDELAYedexpansion

for /f "tokens=* delims= " %%a in (myfile) do (
>> newfile echo %%a
)


=====================================
Helping others achieve escape felicity

M2


Report •


Ask Question