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?

drdot
  • 3,215
  • 9
  • 46
  • 81
  • 3
    use `StringBuilder` instead of `String` for concatenation, once completed convert it to `String` – Saravana Oct 21 '16 at 04:03
  • Possible duplicate of [StringBuilder vs String concatenation in toString() in Java](http://stackoverflow.com/questions/1532461/stringbuilder-vs-string-concatenation-in-tostring-in-java) – Rogue Oct 21 '16 at 04:05
  • Possible duplicate of [Can one initialise a java String with a single repeated character to a specific length](http://stackoverflow.com/questions/1900477/can-one-initialise-a-java-string-with-a-single-repeated-character-to-a-specific) – sprinter Oct 21 '16 at 04:05
  • @saka1029 Except that it will likely create a separate `StringBuilder` for each loop pass. – chrylis -cautiouslyoptimistic- Oct 21 '16 at 04:28

3 Answers3

4

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();
}
Saravana
  • 12,647
  • 2
  • 39
  • 57
  • why is this better? looks like more code – drdot Oct 21 '16 at 04:07
  • 2
    @elixir `String` is immutable in Java, we can't change its value, for every concatenation a new `String` is getting created but `StringBuilder` mutable, we can change its value – Saravana Oct 21 '16 at 04:09
  • @elixir to understand why refer to this link http://stackoverflow.com/questions/1532461/stringbuilder-vs-string-concatenation-in-tostring-in-java posted by Rogue above – kjsebastian Oct 21 '16 at 04:09
  • Two suggestions: 1) eliminate appending `"."` because OP wants `str` concatenated without delimiters; 2) allocate the `StringBuilder` with a specific capacity (`times * str.length()`) to avoid reallocation of its internal buffer. @elixir - Shorter isn't always better. This answer is much more efficient than your code because the code doesn't have to construct a new `String` object and copy the existing string each time through the loop. – Ted Hopp Oct 21 '16 at 04:10
  • @elixir it is explained in [this](http://stackoverflow.com/questions/5234147/why-stringbuilder-when-there-is-string) – Lahiru Ashan Oct 21 '16 at 04:11
  • Oops Ignore my first suggestion above. I misread OP's original post. – Ted Hopp Oct 21 '16 at 04:14
  • Why do you suggest it to be static? – drdot Oct 22 '16 at 18:38
  • @elixir it's a utility method, that's why static – Saravana Oct 23 '16 at 03:21
0

Aside from stringbuilder, if you're repeating a single character it might be faster to just make an array and return a String object:

char repeat = /* some char */;
int amount = /* amount to repeat */;
char[] val = new char[amount];
IntStream.range(0, val.length).forEach(i -> val[i] = repeat);
return new String(val, 0, val.length);

Though if I remember correctly StringBuilder will let you init the underlying array if you want to save that optimization yourself as well:

char repeat = /* some char */;
int amount = /* amount to repeat */;
StringBuilder sb = new StringBuilder(amount); //pass amount to avoid resize
IntStream.range(0, amount).forEach(i -> sb.append(repeat);
return sb.toString();
Rogue
  • 11,105
  • 5
  • 45
  • 71
0

Try this.

static String multiply(String s, int dup) {
    return new String(new char[dup]).replaceAll("\0", s);
}