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]
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.
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.
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.
if you are talking about in the reference of String Class. so you can use
subString/split
for Explode & use String
concate
for Implode.
java.lang.String.split(String regex)
is what you are looking for.
Source: Stackoverflow.com