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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/string_methods("`String Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117448{{"`How to Repeat a String N Times`"}} java/classes_objects -.-> lab-117448{{"`How to Repeat a String N Times`"}} java/class_methods -.-> lab-117448{{"`How to Repeat a String N Times`"}} java/modifiers -.-> lab-117448{{"`How to Repeat a String N Times`"}} java/oop -.-> lab-117448{{"`How to Repeat a String N Times`"}} java/packages_api -.-> lab-117448{{"`How to Repeat a String N Times`"}} java/identifier -.-> lab-117448{{"`How to Repeat a String N Times`"}} java/arrays -.-> lab-117448{{"`How to Repeat a String N Times`"}} java/comments -.-> lab-117448{{"`How to Repeat a String N Times`"}} java/data_types -.-> lab-117448{{"`How to Repeat a String N Times`"}} java/operators -.-> lab-117448{{"`How to Repeat a String N Times`"}} java/output -.-> lab-117448{{"`How to Repeat a String N Times`"}} java/strings -.-> lab-117448{{"`How to Repeat a String N Times`"}} java/variables -.-> lab-117448{{"`How to Repeat a String N Times`"}} java/string_methods -.-> lab-117448{{"`How to Repeat a String N Times`"}} java/system_methods -.-> lab-117448{{"`How to Repeat a String N Times`"}} end

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