Q:

How to multiple strings in java including multiply with 0

I want a primitive that can duplicate a string. It takes the string and the number of duplication as input and generate the output. The number of duplication includes 0. Meaning it will generate empty string. I cannot think of a shorter way to do it besides the one below. The one below uses "." as an example string.
String res = "";
for(int i = 0; i < dup; i++) {
    res = res + ".";
}
return res;

Anyone has a neater solution?

A:

Try this 
private static String multiply(String str, int times) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < times; i++) {
        sb.append(str).append(".");
    }
    return sb.toString();
}

