• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Java equivalent for Perl's join ? ( concat Collection w/separator )

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I often find myself having to concatenate the string values of objects in a collection into a comma-separated string.
I use a StringBuffer and append() in an iteration over the collection, but I have a nagging feeling that there's a built-in operation for this that I'm just not finding.
Is there a way to concatenate a collection of objects' string values into a string, optionally separated by another character(s) a la Perl's join( separator, array ) ?
I'd expect it to be in the Collections framework, but, apart from AbstractCollection's toString() method - which is close - I can't seem to locate it.
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's nothing like this that I am aware of. Of course, you could write a function in a helper class to do this for you.

...but I'm sure you could've done that; I just like typing....
 
Ken Pelletier
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.
Yeah, that's pretty much exactly what I was referring to when I said:
>I use a StringBuffer and append() in an iteration over the collection
So, I take it there *is* no such animal hidden somewhere in the class libraries.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I never found one. I have a pair of functions that I write in every new language I play with:
ArrayToString - turns an array into a delimited string. It picks a delimiter that is not in any of the strings in the array and uses it as the FIRST character of the string. This is not normal.
StringToArray - using the first character as the delimiter (that's why he put it there!) parses the string back into an array.
Because of the delimiter selection logic, you can nest these things until you run out of delimiters.
 
I just had the craziest dream. This tiny ad was in it.
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic