File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ken Pelletier
Ranch Hand
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Joel McNary
Bartender
Posts: 1824
Eclipse IDE Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • 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 agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic