Added join method to StringUtil

This commit is contained in:
Ziver Koc 2015-12-10 21:45:14 +01:00
parent 78b1171754
commit 515bf60309
4 changed files with 33 additions and 3 deletions

View file

@ -27,6 +27,8 @@ package zutil;
import zutil.converters.Converter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
* This is a class whit utility methods.
@ -128,6 +130,23 @@ public class StringUtil {
return str.reverse().toString();
}
/**
* @param list a list of object that toString() will be called on
* @param delimiter a String delimiter that will be added between every entry in the list
* @return a String containing all entries in the list with the specified delimiter in between entries
*/
public static String join(Iterable<?> list, String delimiter){
StringBuilder str = new StringBuilder();
Iterator<?> it = list.iterator();
if(it.hasNext()) {
str.append(it.next());
while (it.hasNext()) {
str.append(delimiter).append(it.next());
}
}
return str.toString();
}
/**
* Trims the given char and whitespace at the beginning and the end
*