Remove the Last Comma

Hi there,

I have a query which will return a list of the items where I have added the comma to separate the string
like this item1, item2, item3, . I wish to know is there any methods I could use to remove the last comma and make it like this
item1, item2, item3 .

This is my current code:-

[I]some query...[/I]
while ( {

if (rs.wasNull()) strDeptList += "";
else strDeptList += ",";
      strDeptList += "'"+rs.getString("DEPART_CODE")+"'";


Please advise. Thanks~

I’d like to assume this is PHP, but to be honest the syntax mimics several different languages; Java and C# for example. The only reason I’m guess PHP is because of .next() in your while loop and I can’t quite think of a C# or Java function with that name. Can you give us a hint?

Either way, you could remove it a couple of ways. Several languages allow you to specify a specific set of characters to trim off the end of a string, using rtrim, TrimRight, RTrim, etc. Or you could go an approach that will always work regardless of language and that is to do a SubString starting at the beginning of the string and going to the String Length - 1 (avoiding the last character).

If this is javascript, you could try the join method.

Does look like js to me. I agree with using join. In PHP I typically build up an array and use implode() for something like this. An array uses a little more memory but I don’t see it as a big deal and it makes for more readable code in my opinion.