...

View Full Version : Replacing spaces



yokelrobin
08-05-2004, 03:33 PM
I need to replace some spaces with commas so that the data can be imported into a spreadsheet.

Normally this would be easy, using Replace(), but in this case, I need to only replace some spaces.

The text string has a random number of spaces in it, and I need to replace the first space and the last three spaces with commas.

For example:

a bcd efg hij klm nop qrs tuv wxy z

Would need to be changed to:

a,bcd efg hij klm nop qrs,tuv,wxy,z

It might also look like this:

a bcde fghijklmn pqr stu vwxyz

which would need to be changed to:

a,bcde fghijklmn,pqr,stu,vwxyz

(to demonstrate random numbers of spaces!)

Any ideas anyone? I'm sure there's a way, but just not sure how exactly!

Thanks in advance.

A1ien51
08-05-2004, 04:15 PM
This is a rough idea on how to code it.

use the split method to split the string at the spaces creatring an array

take the first element and store it into a variable
take the last element and store it in a variable
take the last element-1 and store it in a variable
take the last element-2 and store it in a variable

remove the 1 and the last 3 items from the array (you can store "" into the elements)

Use the join method on the remaining elements on the string amd store it into a variable.

Combine all of the variables seperating them with commas into a large string.

Eric

yokelrobin
08-05-2004, 04:51 PM
Thanks very much - I knew it was a logic thing :)

This is the way I've done it - there may well be better ways, but it works.

Thanks again.



<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set f=fs.OpenTextFile(Server.MapPath("invoices.txt"), 1)

do while f.AtEndOfStream = false

desc = ""

arr = Split(f.ReadLine," ")
top = uBound(arr) - 3

for i = 1 to top
desc = desc & arr(i) & " "
next

desc = Trim(desc)

penultimate = UBound(arr) - 1

response.write arr(0) & "," & desc & "," & arr(UBound(arr)-2) & "," & arr(UBound(arr)-1) & "," & arr(UBound(arr)) & "<br>"

loop

f.Close
Set f=Nothing
Set fs=Nothing
%>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum