Results 1 to 5 of 5
  1. #1
    rovf is offline Member
    Join Date
    Aug 2013
    Posts
    5
    Rep Power
    0

    Default Joining elements of an array - is there an easier way to do it?

    Say I have this:

    String [] a;
    String sep;
    ...

    and I want to join the individual elements together, using a separator sep (i.e. something like a.join(sep) in Ruby, or join($sep,@a) in Perl).

    How to do this best in Java? I found that I could convert tha Array into a ArrayList<String> and then apply the join method, or I can of course manually loop over the array and create the resulting string manually. Both solutions seem to me unnecessary complex for such a simple problem, though. Isn't there a simpler way to do it?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default Re: Joining elements of an array - is there an easier way to do it?

    There is no such method in the Java core classes, so you have to write one yourself; it isn't rocket science ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    rovf is offline Member
    Join Date
    Aug 2013
    Posts
    5
    Rep Power
    0

    Thumbs up Re: Joining elements of an array - is there an easier way to do it?

    Quote Originally Posted by JosAH View Post
    There is no such method in the Java core classes, so you have to write one yourself; it isn't rocket science ...
    For sure I can manage this ;-)

    I just was surprised, that such basic operations are not there already. Perhaps I programmed too long in the Ruby/Perl/Python world.

    Thanks for your quick response!

    Ronald

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default Re: Joining elements of an array - is there an easier way to do it?

    Quote Originally Posted by rovf View Post
    For sure I can manage this ;-)

    I just was surprised, that such basic operations are not there already. Perhaps I programmed too long in the Ruby/Perl/Python world.

    Thanks for your quick response!

    Ronald
    You're welcome; maybe this little method is a fine candidate for your StringUtils class so that you only have to write it once and use it everywhere ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,669
    Rep Power
    5

    Default Re: Joining elements of an array - is there an easier way to do it?

    That's the problem if you compare a development platform to a scripting platform: they have widely different uses (with some overlap of course) and thus a widely different focus in their standard API.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Is there an easier way to get position out of array
    By codyjava in forum New To Java
    Replies: 5
    Last Post: 12-02-2012, 09:05 PM
  2. Sum up elements in an array?
    By D-X69 in forum New To Java
    Replies: 3
    Last Post: 05-30-2012, 04:23 PM
  3. Replies: 3
    Last Post: 11-25-2011, 12:38 AM
  4. keep the first N elements of an array only
    By aneuryzma in forum New To Java
    Replies: 13
    Last Post: 03-27-2011, 04:03 PM
  5. sum of elements in array
    By myst in forum New To Java
    Replies: 7
    Last Post: 07-17-2010, 08:36 AM

Posting Permissions

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