Java String repeat() Method

The String.repeat() method in Java is used to repeat a given string a specified number of times. This method is part of the String class and was introduced in Java 11. It provides a convenient way to create a new string by repeating an existing string.

Table of Contents

  1. Introduction
  2. repeat Method Syntax
  3. Examples
    • Basic Usage
    • Handling Edge Cases
    • Real-World Use Case
  4. Conclusion

Introduction

The String.repeat() method is a member of the String class in Java. It allows you to create a new string by repeating the original string a specified number of times. This method is useful for generating repeated patterns or padding strings.

repeat() Method Syntax

The syntax for the repeat method is as follows:

public String repeat(int count)
  • count: The number of times to repeat the string. If count is zero, an empty string is returned. If count is negative, an IllegalArgumentException is thrown.

Examples

Basic Usage

The repeat method can be used to repeat a string a specified number of times.

Example

public class RepeatExample {
    public static void main(String[] args) {
        String str = "Hello";
        String repeatedStr = str.repeat(3);
        System.out.println("Original string: " + str);
        System.out.println("Repeated string: " + repeatedStr);
    }
}

Output:

Original string: Hello
Repeated string: HelloHelloHello

Handling Edge Cases

Example: Repeating Zero Times

If the repeat count is zero, the repeat method returns an empty string.

public class RepeatZeroTimesExample {
    public static void main(String[] args) {
        String str = "Hello";
        String repeatedStr = str.repeat(0);
        System.out.println("Original string: " + str);
        System.out.println("Repeated string: '" + repeatedStr + "'");
    }
}

Output:

Original string: Hello
Repeated string: ''

Example: Repeating a Negative Number of Times

If the repeat count is negative, the repeat method throws an IllegalArgumentException.

public class RepeatNegativeTimesExample {
    public static void main(String[] args) {
        String str = "Hello";
        try {
            String repeatedStr = str.repeat(-1);
            System.out.println("Repeated string: " + repeatedStr);
        } catch (IllegalArgumentException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}

Output:

Error: count is negative

Real-World Use Case

Example: Creating a Padding String

One common use case for repeat is creating a padding string to align text output.

public class PaddingExample {
    public static void main(String[] args) {
        String str = "Java";
        int paddingSize = 10;
        String paddedStr = str + " ".repeat(paddingSize - str.length());
        System.out.println("'" + paddedStr + "'");
    }
}

Output:

'Java      '

In this example, the repeat method is used to add spaces after the string "Java" to create a padded string of a fixed length.

Example: Generating a Simple Pattern

The repeat method can be used to generate simple patterns, such as a line of dashes or stars.

public class PatternExample {
    public static void main(String[] args) {
        String pattern = "-".repeat(20);
        System.out.println(pattern);
    }
}

Output:

--------------------

In this example, the repeat method is used to create a line of 20 dashes.

Conclusion

The String.repeat() method in Java is a powerful and convenient tool for repeating a string a specified number of times. It is useful for generating repeated patterns, padding strings, and other scenarios where repeated text is needed. By understanding and utilizing the repeat method, you can efficiently manage string repetition tasks in your Java programs.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top