w3resource

Java: Concatenate a given string with itself of a given number of times


106. Repeat String N Times

Write a Java program to concatenate a given string with itself a given number of times.

Visual Presentation:

Java String Exercises: Concatenate a given string with itself of a given number of times


Sample Solution:

Java Code:

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        String str1 = "PHP"; // Declare and initialize a string variable
        System.out.println("Original string: " + str1); // Display the original string
        String resultV1 = repeat_str(str1, 7); // Call the repeat_str method with str1 and 7 as arguments
        System.out.println("\nAfter repeating 7 times: " + resultV1); // Display the result of repeating str1 7 times
    }

    public static String repeat_str(String str1, int n) {
        // Check if the input string is null or empty
        if (str1 == null || str1.isEmpty()) {
            return ""; // Return an empty string
        }
        
        // Check if n is less than or equal to 0
        if (n <= 0) {
            return str1; // Return the original string as n is 0 or negative
        }
        
        // Check if multiplying the length of str1 by n will exceed the maximum size of a String
        if (Integer.MAX_VALUE / n < str1.length()) {
            throw new OutOfMemoryError("Maximum size of a String will be exceeded"); // Throw an error
        }
        
        StringBuilder x = new StringBuilder(str1.length() * n); // Create a StringBuilder to store the result
        // Repeat the string n times and append it to the StringBuilder
        for (int i = 1; i <= n; i++) {
            x.append(str1);
        }
        return x.toString(); // Return the final repeated string
    }
}

Sample Output:

Original string: PHP

After repeating 7 times: PHPPHPPHPPHPPHPPHPPHP

Flowchart:

Flowchart: Java String Exercises - Concatenate a given string with itself of a given number of times.



For more Practice: Solve these Related Problems:

  • Write a Java program to generate a new string by repeating a base string n times without using the built-in repeat() method.
  • Write a Java program to concatenate a string with itself a specified number of times using a loop.
  • Write a Java program to form a composite string by appending multiple copies of a string.
  • Write a Java program to repeat a string repeatedly and join the repetitions to form the final string.

Go to:


PREV : Count Substring Occurrences.
NEXT : Count Character Occurrences.

Java Code Editor:

Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.