Java equivalent to Explode and Implode(PHP)

JavaArraysStringSplit

Java Problem Overview


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]

Java Solutions


Solution 1 - Java

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();

Solution 2 - Java

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.

Solution 3 - Java

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.

Solution 4 - Java

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

Solution 5 - Java

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.

Solution 6 - Java

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

> subString/split

for Explode & use String

> concate

for Implode.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPankaj WanjariView Question on Stackoverflow
Solution 1 - JavaBrian RoachView Answer on Stackoverflow
Solution 2 - JavaMennoView Answer on Stackoverflow
Solution 3 - JavaEnKryptView Answer on Stackoverflow
Solution 4 - Javauser1907906View Answer on Stackoverflow
Solution 5 - JavaIvan SharametView Answer on Stackoverflow
Solution 6 - JavaSaunik SinghView Answer on Stackoverflow