Computing.Net > Forums > Programming > To trim trailing spaces from a string

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

To trim trailing spaces from a string

Reply to Message Icon

Name: Gods
Date: October 21, 2009 at 08:07:14 Pacific
OS: Windows XP
CPU/Ram: 2.992 GHz / 1021 MB
Product: Dell / Optiplex gx270
Subcategory: Batch
Comment:

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 "



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: October 21, 2009 at 09:00:46 Pacific
Reply:

@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


0

Response Number 2
Name: Gods
Date: October 27, 2009 at 04:23:18 Pacific
Reply:

Thanks Mechanix ... this wrks perfectly fine :))


0

Response Number 3
Name: shiva4palz
Date: October 29, 2009 at 11:24:12 Pacific
Reply:

Hey M2,

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


0

Response Number 4
Name: gtaion
Date: October 29, 2009 at 21:08:16 Pacific
Reply:

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


0

Response Number 5
Name: Mechanix2Go
Date: October 30, 2009 at 05:35:23 Pacific
Reply:

@echo off > newfile & setLocal enableDELAYedexpansion

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


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

M2


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: To trim trailing spaces from a string

trimming spaces in a string www.computing.net/answers/programming/trimming-spaces-in-a-string/1025.html

VB6 Opening a Form from a string in code www.computing.net/answers/programming/vb6-opening-a-form-from-a-string-in-code/2774.html

extracting number from a string SQL www.computing.net/answers/programming/extracting-number-from-a-string-sql/3902.html