#1
  1. No Profile Picture
    Contributing User
    ASP Explorer (0 - 99 posts)

    Join Date
    Jan 2014
    Posts
    69
    Rep Power
    5

    Remove last comma?


    Hi guys

    I'm hoping there's an easy solution to this.

    Basically, I'm querying my database table to display a list of top 5 car models and then lopping through the set using something like this:


    ------------------------------
    <%
    Do while not oRsBJ.eof
    %>

    <%=oRsBJ("model")%>,

    <%
    oRsBJ.MoveNext
    loop
    %>
    ------------------------------

    So, on my page it will display something like this


    ------------------------------

    DB9, Vanquish, Vantage, DB7, Rapide,

    ------------------------------

    The problem I have is that I wish to construct a sentence and then show the five models within it like so:

    ------------------------------
    The top five Aston Martin models are the DB9, Vanquish, Vantage, DB7 and Rapide.
    ------------------------------

    I have therefore tried:

    ------------------------------

    The top five Aston Martin models are the
    <%
    Do while not oRsBJ.eof
    %>

    <%=oRsBJ("model")%>,

    <%
    oRsBJ.MoveNext
    loop
    %>
    .
    ------------------------------

    However, the output on my page is this, which isn't great:

    ------------------------------
    The top five Aston Martin models are the DB9, Vanquish, Vantage, DB7, Rapide,.
    ------------------------------

    So, you can probably guess what my question is. Basically, how can I construct my sentence so it removes the last comma and secondly, if at all possible, replace the second from last comma with an "and".

    Is this at all possible? If so, how can it be done?

    Any help would be fully appreciated

    Best regards

    Rod from the UK
  2. #2
  3. Contributing User
    ASP Skiller (1500 - 1999 posts)

    Join Date
    Mar 2005
    Location
    Columbus, OH
    Posts
    1,665
    Rep Power
    284
    the simple way is below
    try
    Code:
    <%
    'create variable
    models = ""
    Do while not oRsBJ.eof
    ' add models into variable
    models = models & oRsBJ("model") & ", "
    oRsBJ.MoveNext
    loop
    remove last ", "
    models = Left(models, len(models) - 2)
    ' if you want to add and
    models = models & " and"
    
    response.write  models 
    
    %>

    Comments on this post

    • rjoseph244510 agrees
    GK
    __________________________________________________ _____
    if you found this post is useful click Give Rep button (bottom side on this reply ) and agree
  4. #3
  5. No Profile Picture
    Contributing User
    ASP Explorer (0 - 99 posts)

    Join Date
    Jan 2014
    Posts
    69
    Rep Power
    5
    Excellent!

    Thanks so much GK

    Best regards

    Rod from the UK
  6. #4
  7. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Apr 2017
    Posts
    5
    Rep Power
    0
    Originally Posted by rjoseph244510
    Excellent!

    Thanks so much GK

    Best regards

    Rod from the UK

    Best code:

    Do while not oRsBJ.eof
    response.write oRsBJ("model")
    oRsBJ.movenext
    if not oRsBJ.eof then response.write ", "
    Loop

Similar Threads

  1. Remove comma
    By todd2006 in forum .NET Development
    Replies: 3
    Last Post: October 5th, 2011, 07:12 AM
  2. Remove comma at end of array.
    By invision in forum ASP Development
    Replies: 10
    Last Post: February 8th, 2011, 07:19 AM
  3. I want to add comma at end of....
    By asifbhura in forum .NET Development
    Replies: 2
    Last Post: April 30th, 2006, 06:37 AM
  4. How to get rid of this comma!!?!
    By gilgalbiblewhee in forum ASP Development
    Replies: 10
    Last Post: August 2nd, 2005, 04:00 PM
  5. comma
    By sam_sam in forum ASP Development
    Replies: 4
    Last Post: May 20th, 2005, 01:18 PM

IMN logo majestic logo threadwatch logo seochat tools logo