Explode and Implode (like PHP) in Java

I am new to Java, although I had good experience in PHP and am looking for the perfect replacement for the Explode and implode functions (available in PHP) in Java.

I have Googled for the same, but not happy with the results. Anyone have a good solution for my problem would be appreciated.

+74
java string arrays split
May 27 '13 at 12:50
source share
6 answers

Javadoc for String shows that String.split() is what you are looking for in relation to explode .

Java does not include the "implode" equivalent of "join". Instead of including a giant external dependency for a simple function, as other answers suggest, you can simply write a couple lines of code. There are several ways to do this; using StringBuilder is one:

 String foo = "This,that,other"; String[] split = foo.split(","); StringBuilder sb = new StringBuilder(); for (int i = 0; i < split.length; i++) { sb.append(split[i]); if (i != split.length - 1) { sb.append(" "); } } String joined = sb.toString(); 
+112
May 27 '13 at
source share

String.split() can provide you a replacement for explode()

To replace implode() I would advise you to write either a user-defined function or use the Apache Commons StringUtils.join() functions.

+19
May 27 '13 at 12:55
source share

Good alternatives are the String.split and StringUtils.join methods.

Explosion:

 String[] exploded="Hello World".split(" "); 

Implode:

 String imploded=StringUtils.join(new String[] {"Hello", "World"}, " "); 

Keep in mind that StringUtils is in an external library.

+10
May 27 '13 at 12:57
source share

java.lang.String.split(String regex) is what you are looking for.

+3
May 27 '13 at 12:55
source share

I am not familiar with PHP, but I think String.split is Java, equivalent to PHP explode . As for implode , the standard library does not provide such functionality. You just iterate over the array and build the string using StringBuilder / StringBuffer . Or you can try the excellent Google Guava Splitter and Joiner or split/join from Apache Commons StringUtils .

+3
May 27 '13 at 13:02
source share

if you speak in reference to String Class. so you can use

substring / split

for Explode and using String

concate

for implode.

-one
May 27 '13 at 12:55
source share



All Articles