• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

best way to remove the last comma

 
Ranch Hand
Posts: 563
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
what is the best way to remove the last comma in my String ?

for (int i=0;i<5,i++)
{
// some pre-treatment here

String foo = "hello";
String result += foo + ", ";

// and some post-treatment here

}

result = hello, hello, hello, hello, hello,
but I want it to be:
result = hello, hello, hello, hello, hello
(without the last comma)

Thanks
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Max longbeach:
Hi,
what is the best way to remove the last comma in my String ?

for (int i=0;i<5,i++)
{
// some pre-treatment here

String foo = "hello";
String result += foo + ", ";

// and some post-treatment here

}

result = hello, hello, hello, hello, hello,
but I want it to be:
result = hello, hello, hello, hello, hello
(without the last comma)

Thanks



 
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Bartender
Posts: 1843
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indeed, the best way to remove it is to not put it in in the first place. But if you need to remove it, you can say:



This will chomp the last character in the string.
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd also recommend using a StringBuffer/StringBuilder if you're going to be repeatedly concatenating a string...
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at String.endsWith() too. You might want to check to see if the comma is there before you remove it.

Let's look into never adding the extra comma. I see this kind of thing often when building delimited lists like yours:

I prefer to shortcut a bit:

If you can modify the code that generated the list, see if that helps.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My preference is a slight variation of Joanne's suggestion:


[ August 12, 2005: Message edited by: Layne Lund ]
[ August 12, 2005: Message edited by: Layne Lund ]
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The old "priming the pump" option. One line of code before the loop eliminates a redundant assignment statement every time through the loop. I like that, too.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic