How to Repeat a String N Times

JavaJavaBeginner
Practice Now

Introduction

In this lab, we will learn how to repeat a string N number of times in Java. We will cover three ways to repeat a string using Java 8, Java 11, and the String Constructor, and replace() method. The Java 11 method is the simplest way to repeat the string directly from the String class. The String Constructor and replace() method requires some logical code to repeat a string. The Java 8 method uses the nCopies() method of the Collections class and joins it using the join() method of the String class.

Create a Java File

Create a .java file in the directory ~/project using the following command:

touch ~/project/RepeatString.java

Repeat String using Java 11

In Java 11, repeat a string using the repeat() method of the String class as follows:

public class RepeatString {
	public static void main(String[] args){
		String str = "Studytonight";
		System.out.println(str);
		// Repeat String
		String newStr = str.repeat(3);
		System.out.println(newStr);
	}
}

To run the code, go to the directory containing the RepeatString.java file and compile it using the following command:

javac RepeatString.java

After the code is compiled successfully, run the compiled class file using the following command:

java RepeatString

The output will be as follows:

Studytonight
StudytonightStudytonightStudytonight

Repeat String using String Constructor and replace() Method

In this method, we create a string using a char array and replace the array's default value with the provided string using the replace() method. The following code demonstrates how to repeat a string using this method:

public class RepeatString {
	public static void main(String[] args){
		String str = "Studytonight";
		System.out.println(str);
		// Repeat String
		String newStr = new String(new char[3]).replace("\0", str);
		System.out.println(newStr);
	}
}

To run the code, go to the directory containing the RepeatString.java file and compile it using the following command:

javac RepeatString.java

After the code is compiled successfully, run the compiled class file using the following command:

java RepeatString

The output will be as follows:

Studytonight
StudytonightStudytonightStudytonight

Repeat String using Java 8

In Java 8, repeat a string using the nCopies() method of the Collections class. Then, join the repeating strings using the join() method of the String class. The following code demonstrates how to repeat a string using this method:

import java.util.Collections;

public class RepeatString {
	public static void main(String[] args){
		String str = "Studytonight";
		System.out.println(str);
		// Repeat String
		String newStr = String.join("", Collections.nCopies(3, str));
		System.out.println(newStr);
	}
}

To run the code, go to the directory containing the RepeatString.java file and compile it using the following command:

javac RepeatString.java

After the code is compiled successfully, run the compiled class file using the following command:

java RepeatString

The output will be as follows:

Studytonight
StudytonightStudytonightStudytonight

Summary

In this lab, we learned how to repeat a string N number of times using Java. We covered three methods for repeating a string which are Java 11 repeat() method, the String Constructor and replace() method, and the Java 8 nCopies() method of the Collections class and join() method of the String class. Now, you can use any of the above methods to repeat a string in Java.

Other Java Tutorials you may like