Created
September 10, 2008 07:57
-
-
Save monjudoh/9849 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* join function for Java | |
* | |
* example | |
* List<String> list = new ArrayList<String>(); | |
* list.add("hogehoge"); | |
* list.add("fugafuga"); | |
* list.add("piyopiyo"); | |
* join(list,"&");// hogehoge&fugafuga&piyopiyo | |
* | |
* String[] array = {"foo", "buzz", "bar"}; | |
* join(array,"&");// foo&buzz&bar | |
*/ | |
public static String join(List<String> list,String delimiter){ | |
delimiter = delimiter != null ? delimiter : ""; | |
String[] array = list.toArray(new String[list.size()]); | |
return join(array,delimiter); | |
} | |
public static String join(String[] array, String delimiter) { | |
StringBuilder temp = new StringBuilder(); | |
for(int i = 0; i < array.length; i++){ | |
temp.append(array[i]); | |
if(i < array.length - 1){ | |
temp.append(delimiter); | |
} | |
} | |
return temp.toString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment