Java 11 - String repeat() method example

Java 11 added a few useful APIs to the commonly used String class.  String repeat() method one of them. In this post, we will see the usage of String repeat() method with an example.
Check out New String APIs/Methods in Java 11 (JDK11) with Examples
String java.lang.String.repeat(int count) - As the name suggests, the repeat() instance method repeats the string content.

String repeat() API - Java 11

As the name suggests, the repeat() instance method repeats the string content.
It returns a string whose value is the concatenation of the string repeated n times, where n is passed as a parameter:
package net.javaguides.examples;

/**
 * Java 11 (JDK11) New String APIs/Methods with Examples
 * @author Ramesh Fadatare
 *
 */
public class StringAPIInJDK11 {
    public static void main(String[] args) {

        String input = "Java";
        /**
         *  the repeat() instance method repeats the string content
         */
        String output = input.repeat(2) + "Guides".repeat(3);
        System.out.println(output); // Output: JavaJavaGuidesGuidesGuides
    }
}
Output:
JavaJavaGuidesGuidesGuides 

Reference

Comments