Wednesday, 9 May 2018

Java: function for arrays like PHP's join()?



I want to join a String[] with a glue string. Is there a function for this?


Answer



Starting from Java8 it is possible to use String.join().



String.join(", ", new String[]{"Hello", "World", "!"})



Generates:



Hello, World, !


Otherwise, Apache Commons Lang has a StringUtils class which has a join function which will join arrays together to make a String.



For example:




StringUtils.join(new String[] {"Hello", "World", "!"}, ", ")


Generates the following String:



Hello, World, !

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...