Close

Java - How to repeat a string n number of times?

[Updated: Apr 28, 2020, Created: Apr 28, 2020]

Java String Manipulation Java 

This tutorial shows different ways to repeat an input string n number of times.

By using InputStream#range()

package com.logicbig.example;

import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class StringRepeat {
  public static void main(String[] args) {
      String s = stringRepeat("=", 10);
      System.out.println(s);
  }

  private static String stringRepeat(String input, int n) {
      return IntStream.range(0, n)
                      .mapToObj(i -> input)
                      .collect(Collectors.joining());
  }
}
==========

By using Stream#generate()

package com.logicbig.example;

import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StringRepeat1 {
  public static void main(String[] args) {
      String s = stringRepeat("=", 10);
      System.out.println(s);
  }

  private static String stringRepeat(String input, int n) {
      return Stream.generate(() -> input)
                   .limit(n)
                   .collect(Collectors.joining());
  }
}
==========

By using Collections#nCopies()

package com.logicbig.example;

import java.util.Collections;
import java.util.stream.Collectors;

public class StringRepeat2 {
  public static void main(String[] args) {
      String s = stringRepeat("=", 10);
      System.out.println(s);
  }

  private static String stringRepeat(String input, int n) {
      return Collections.nCopies(n, input)
                        .stream()
                        .collect(Collectors.joining());

  }
}
==========

By using StringBuilder#append()

package com.logicbig.example;

public class StringRepeat3 {
  public static void main(String[] args) {
      String s = stringRepeat("=", 10);
      System.out.println(s);
  }

  private static String stringRepeat(String input, int n) {
      StringBuilder sb = new StringBuilder();
      for (int i = 0; i < n; i++) {
          sb.append(input);
      }
      return sb.toString();
  }
}
==========

By using Java 11 String#repeat()

package com.logicbig.example;

public class StringRepeat4 {
  public static void main(String[] args) {
      String s = stringRepeat("=", 10);
      System.out.println(s);
  }

  private static String stringRepeat(String input, int n) {
      return input.repeat(n);
  }
}
==========

By using Apache common-lang's StringUtils#repeat()

pom.xml

<dependency>
   <groupId>org.apache.commons</groupId>
   <artifactId>commons-lang3</artifactId>
   <version>3.10</version>
</dependency>
package com.logicbig.example;

import org.apache.commons.lang3.StringUtils;

public class StringRepeat6 {
  public static void main(String[] args) {
      String s = stringRepeat("=", 10);
      System.out.println(s);
  }

  private static String stringRepeat(String input, int n) {
      return StringUtils.repeat(input, n);
  }
}
==========

By using String#concat() recursively

package com.logicbig.example;

public class StringRepeat8 {
  public static void main(String[] args) {
      String s = stringRepeat("=", 10);
      System.out.println(s);
  }

  private static String stringRepeat(String input, int n) {
      return n > 1 ?
              input.concat(stringRepeat(input, n - 1)) :
              input;
  }
}
==========

By using String#concat() in a loop

package com.logicbig.example;

public class StringRepeat9 {
  public static void main(String[] args) {
      String s = stringRepeat("=", 10);
      System.out.println(s);
  }

  private static String stringRepeat(String input, int n) {
      String result = "";
      for (int i = 0; i < n; i++) {
          result = result.concat(input);
      }
      return result;
  }
}
==========

By using Arrays#fill()

package com.logicbig.example;

import java.util.Arrays;
import java.util.stream.Collectors;

public class StringRepeat10 {
  public static void main(String[] args) {
      String s = stringRepeat("=", 10);
      System.out.println(s);
  }

  private static String stringRepeat(String input, int n) {
      String[] strings = new String[n];
      Arrays.fill(strings, input);
      return Arrays.stream(strings).collect(Collectors.joining());
  }
}
==========

See Also