SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2011
    Location
    Kuching, Sarawak
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation 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:-

    Code:
    some query...
    while (rs.next()) {
    
    rs.getString("DEPART_CODE"); 
    if (rs.wasNull()) strDeptList += ""; 
    else strDeptList += ",";
          strDeptList += "'"+rs.getString("DEPART_CODE")+"'";
    
    }
    Please advise. Thanks~

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,067
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    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).
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  3. #3
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    If this is javascript, you could try the join method.

    http://www.w3schools.com/jsref/jsref_join.asp
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  4. #4
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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.
    - Robert


Tags for this Thread

Bookmarks

Posting Permissions

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