Created
March 21, 2013 22:31
-
-
Save LarryBattle/5217402 to your computer and use it in GitHub Desktop.
Join ArrayList in java. Combines all the elements into a string separated the `delimiter`.
This file contains hidden or 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
/** | |
* Combines all the elements into a string seperated the `delimiter`. | |
* Same as `Array.prototype.join()` in Javascript. | |
* @example | |
ArrayList<String> x = new ArrayList<String>(); | |
x.add("1"); | |
x.add("2"); | |
joinArrayListString(x, ",").equals("1,2") == true; | |
*/ | |
public String joinArrayListString(ArrayList<String> r, String delimiter) { | |
if(r == null || r.size() == 0 ){ | |
return ""; | |
} | |
StringBuffer sb = new StringBuffer(); | |
int i, len = r.size() - 1; | |
for (i = 0; i < len; i++){ | |
sb.append(r.get(i) + delimiter); | |
} | |
return sb.toString() + r.get(i); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment