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

I’ve used something like this to list names of individuals beneath group photos on web pages. Perhaps it might be useful to you. Note that the magenta outline is for test purposes only and is intended to be deleted.

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>template</title>
<!--
https://www.sitepoint.com/community/t/remove-last-comma/214476
rjoseph244510
Feb 5,2016 4:11 AM
-->
    <style type="text/css">

ul.linelist {
    list-style:none;
    padding:0;
    margin:0;
}
.linelist li {
    display:inline;
    font-weight:bold;
    font-size:1.0625em;
    font-family:'Trebuchet MS',sans-serif;
    outline:1px solid magenta;  /* TEST Outline. TO BE DELETED */
}
.linelist li + li::after {
    content:",";
    display:inline;
}
.linelist li:nth-last-child(2)::after {
    content:"";
}
.linelist li + li:last-child::before {
    content:"and ";
}
.linelist li + li:last-child::after {
    content:".";
}

    </style>
</head>
<body>

<ul class="linelist">
    <li>The top five Aston Martin models are the</li>
    <li>DB9</li>
    <li>Vanquish</li>
    <li>Vantage</li>
    <li>DB7</li>
    <li>Rapide</li>
</ul>

</body>
</html>

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.