スキップしてメイン コンテンツに移動

投稿

ラベル(join)が付いた投稿を表示しています

Java: Joining String Array with Separator (Equilvarent to PHP implode)

If you would like to join array with a separator, what will you do? e.g. Joining "a","b","c" with separator "," to "a,b,c". In PHP, you just use implode function. Actually PHP has a lot of functions like manipulating, converting string and array. In java, these kind of functions are not provided in official JDK. If you are familiar with Java Libraries, you just use them. - Joiner in google Guava, StringUtils in Apache Coommons. I will show you quick code snippet for jonining or imploding java string array with a separator. private static String join(String[] pieces, String separator) { if(pieces.length == 0) return ""; StringBuilder sb = new StringBuilder(pieces[0]); for(int i = 1; i < pieces.length; i++) { sb.append(separator).append(pieces[i]); } return sb.toString(); } If the number of array elements is zero, the method returns empty string, but if you would not prefer this...