Java equivalent to Explode and Implode(PHP)
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();
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.
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.