Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Aug 2002
    Thanked 0 Times in 0 Posts

    Replacing spaces

    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.

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Between DC and Baltimore In a Cave
    Thanked 94 Times in 88 Posts


    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.

    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    New Coder
    Join Date
    Aug 2002
    Thanked 0 Times in 0 Posts
    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) & " "
    desc = Trim(desc)
    penultimate = UBound(arr) - 1
    response.write arr(0) & "," & desc & "," & arr(UBound(arr)-2) & "," & arr(UBound(arr)-1) & "," & arr(UBound(arr)) & "<br>"
    Set f=Nothing
    Set fs=Nothing
    Last edited by yokelrobin; 08-05-2004 at 05:08 PM. Reason: minor code change


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts