HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java 11 / String repeat() – Repeat string N times in Java

String repeat() – Repeat string N times in Java

Learn to repeat a given string N times, to produce a new string which contains all the repetitions, though a simple Java program. We will use method Sting.repeat(N) (since Java 11) and using regular expression which can be used till Java 10.

1. String.repeat() API [Since Java 11]

This method returns a string whose value is the concatenation of given string repeated count times. If the string is empty or count is zero then the empty string is returned.

1.1. Syntax

/**
* Parameters:
* count - number of times to repeat
* 
* Returns:
* A string composed of this string repeated count times or the empty string if this string is empty or count is zero
* 
* Throws:
* IllegalArgumentException - if the count is negative.
*/

public String repeat​(int count)

1.2. Example

Java program to repeat string ‘Abc’ to 3 times.

public class Main 
{
	public static void main(String[] args) 
	{
		String str = "Abc";

		System.out.println( str.repeat(3) );
	}
}

Program output.

AbcAbcAbc

2. Repeat string using regex [till Java 10]

If you are working on JDK <= 10, then you may consider using regex to repeat a string N times.

Java program to repeat string ‘Abc’ to 3 times.

public class Main 
{
	public static void main(String[] args) 
	{
		String str = "Abc";

		String repeated = new String(new char[3]).replace("\0", str);

		System.out.println(repeated);
	}
}

Program output.

AbcAbcAbc

3. Apache Common’s StringUtils class

If regex is not what you are looking for – then you can use StringUtils class and it’s method repeat(times).

import org.apache.commons.lang3.StringUtils;

public class Main 
{
	public static void main(String[] args) 
	{
		String str = "Abc";
	
		String repeated = StringUtils.repeat(str, 3);

		System.out.println(repeated);
	}
}

Program output.

AbcAbcAbc

Drop me your questions related to how to repeat a string N times in Java.

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Henry H Kim

    June 1, 2020

    Are you saying that regex can be used for jdk <=14, but it is recommended only if jdk <= 10, since 'repeat's is avail in jdk 11?

    How you wrote this implies jdk 11 will not work for regex.
    "If you are working on JDK <= 10, then you may consider using regex to repeat a string N times."

    • Lokesh Gupta

      June 2, 2020

      Yes, repeat() is recommeneded JDK 11 and later.

  2. Aravind Balaji

    November 18, 2019

    What is the solution, when the n times is of data type Long. The above mentioned methods are not supporting long data type. Only int data types are supported. Can you help me with this.

Comments are closed on this article!

Search Tutorials

Java 11 Tutorial

  • Java 11 – New features
  • Java 11 – String.isBlank()
  • Java 11 – String.lines()
  • Java 11 – String.repeat()
  • Java 11 – String.strip()
  • Java 11 – Files.readString()
  • Java 11 – Files.writeString()

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces