Added more functionality to the string utilities.
*/
package org.linqs.psl.util;
+import java.util.List;
+
/**
* Various static String utilities.
*/
@@ -64,6 +66,28 @@ public final class StringUtils {
return doubles;
}
+ public static String join(List<?> parts) {
+ return join(parts, DEFAULT_DELIM);
+ }
+
+ public static String join(List<?> parts, char delim) {
+ return join(parts, "" + delim);
+ }
+
+ public static String join(List<?> parts, String delim) {
+ StringBuilder builder = new StringBuilder(parts.size() * 2 - 1);
+
+ for (int i = 0; i < parts.size(); i++) {
+ builder.append(parts.get(i));
+
+ if (i != parts.size() - 1) {
+ builder.append(delim);
+ }
+ }
+
+ return builder.toString();
+ }
+
public static String join(Object[] parts) {
return join(parts, DEFAULT_DELIM);
}
@@ -129,4 +153,27 @@ public final class StringUtils {
return builder.toString();
}
+
+ public static String repeat(String text, int times) {
+ return repeat(text, "", times);
+ }
+
+ public static String repeat(String text, String delim, int times) {
+ if (times < 0) {
+ throw new IllegalArgumentException("Cannot repeat a string negative times.");
+ } else if (times == 0) {
+ return "";
+ }
+
+ StringBuilder builder = new StringBuilder(times * 2);
+ for (int i = 0; i < times; i++) {
+ builder.append(text);
+
+ if (i != (times - 1)) {
+ builder.append(delim);
+ }
+ }
+
+ return builder.toString();
+ }
}
