104

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

I have Googled for the same but not satisfied with the results. Anyone has the good solution for my problem will be appreciated.

For example:

String s = "x,y,z";
//Here I need a function to divide the string into an array based on a character.
array a = javaExplode(',', s);  //What is javaExplode?
System.out.println(Arrays.toString(a));

Desired output:

[x, y, z]
Brugui
  • 574
  • 6
  • 20
Pankaj Wanjari
  • 1,275
  • 2
  • 9
  • 11
  • 3
    OK, what solutions have you found so far ? – Konstantin Yovkov May 27 '13 at 12:50
  • This is practically the same as [A method to reverse effect of java String.split()?](http://stackoverflow.com/questions/794248) – PhoneixS Jul 25 '13 at 15:24
  • 7
    why this question is down voted ? and off topic ?? Please suggest to avoid for next questions. – Pankaj Wanjari Aug 31 '13 at 07:07
  • 21
    I think based on the view count this question is not that bad at all. I agree it is a weird question but a lot of us PHP programmers just google for java explode and then get this as an correct answer. – gorgi93 Apr 25 '14 at 21:45
  • 7
    Now it's November 2014, and I googled java string explode, and this question still came out as the top result. I think the status 'closed as off-topic' should be lifted, as this is not off topic at all for me. – Chen Li Yong Nov 07 '14 at 04:24
  • 3
    February 2016, this question is the main result for "java explode". – undefined Feb 18 '16 at 03:10
  • I think they just want desired code and desired results with an example. – john k Feb 03 '18 at 23:58

6 Answers6

151

The Javadoc for String reveals that String.split() is what you're looking for in regard to explode.

Java does not include a "implode" of "join" equivalent. Rather than including a giant external dependency for a simple function as the other answers suggest, you may just want to write a couple lines of code. There's a number of ways to accomplish that; using a 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();
Brian Roach
  • 76,169
  • 12
  • 136
  • 161
  • Thanks u guys for all your answers . – Pankaj Wanjari May 28 '13 at 05:55
  • 2
    Split does indeed use a regular expression (in PHP, Perl, Java, etc.). If you want to split on another symbol, it needs to be escaped, like this: String[] split = foo.split("\\\("); – HoldOffHunger Jun 04 '17 at 22:40
  • 2
    Note that since **Java 8** we actually have `String#join` ([documentation](https://docs.oracle.com/javase/9/docs/api/java/lang/String.html#join-java.lang.CharSequence-java.lang.CharSequence...-)). For your example: `String joined = String.join(" ", split);`. The method uses the `StringJoiner` class. – Zabuzard Feb 04 '18 at 04:57
25

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

For a replacement of implode() I'd advice you to write either a custom function or use Apache Commons's StringUtils.join() functions.

Menno
  • 12,175
  • 14
  • 56
  • 88
17

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

Explode :

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

Implode :

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

Keep in mind though that StringUtils is in an external library.

EnKrypt
  • 777
  • 8
  • 23
  • 1
    Where do you find StringUtils? I find this class in com.sun.internal and com.sun.deploy but I don't think that it is a good idea to directly use classes under com.sun `packages such as sun., that are outside of the Java platform, can be different across OS platforms (Solaris, Windows, Linux, Macintosh, etc.) and can change at any time without notice with SDK versions` – Arnaud Denoyelle May 27 '13 at 13:00
  • Ok, after your edit, I see that this class comes from an external lib – Arnaud Denoyelle May 27 '13 at 13:03
  • ...which is [Appache Commons](https://commons.apache.org/) (I guess): [StringUtils](https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html) – crusy Aug 23 '17 at 09:29
5

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

4

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

Ivan Sharamet
  • 317
  • 4
  • 15
  • 1
    All links are valid except for Guava, since 3 years passed and they moved to GitHub. I've updated the comment with new links. – Ivan Sharamet May 23 '16 at 06:49
-1

if you are talking about in the reference of String Class. so you can use

subString/split

for Explode & use String

concate

for Implode.

Saunik Singh
  • 996
  • 1
  • 9
  • 19