Java StringBuffer repeat() Method

The StringBuffer.repeat() method in Java is used to create a new string that is a repetition of the original string a specified number of times. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality. We will also cover a real-world use case to show how StringBuffer.repeat() can be used effectively.

Table of Contents

  1. Introduction
  2. repeat Method Syntax
  3. Examples
    • Repeating a String
    • Handling Zero and Negative Repetitions
  4. Real-World Use Case
    • Example: Generating a Pattern
  5. Conclusion

Introduction

The repeat() method is not part of the StringBuffer class. Instead, String.repeat() method, introduced in Java 11, can be used to achieve the same functionality with strings. This guide will show how to repeat a string using String.repeat() and then append it to a StringBuffer.

repeat Method Syntax

The syntax for the repeat method is as follows:

public String repeat(int count)
  • Parameters:
    • count: The number of times to repeat the string.
  • Returns: A new string that is a concatenation of the original string repeated count times.

Examples

Repeating a String

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";

        // Repeating the string 3 times
        String repeatedStr = str.repeat(3);

        // Printing the repeated string
        System.out.println(repeatedStr);
    }
}

Output:

HelloHelloHello

Handling Zero and Negative Repetitions

If the repetition count is zero, the method returns an empty string. If the repetition count is negative, the method throws an IllegalArgumentException.

Example

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

        // Repeating the string 0 times
        String repeatedStrZero = str.repeat(0);
        System.out.println("Repeated 0 times: '" + repeatedStrZero + "'");

        try {
            // Repeating the string -1 times
            String repeatedStrNegative = str.repeat(-1);
            System.out.println("Repeated -1 times: '" + repeatedStrNegative + "'");
        } catch (IllegalArgumentException e) {
            System.out.println("Exception: " + e.getMessage());
        }
    }
}

Output:

Repeated 0 times: ''
Exception: count is negative: -1

Real-World Use Case

Example: Generating a Pattern

A common real-world use case for String.repeat() is generating patterns, such as creating a line of repeated characters for formatting purposes.

Example

public class PatternGenerator {
    public static void main(String[] args) {
        String line = "-";

        // Generating a line of 50 dashes
        String pattern = line.repeat(50);

        // Printing the generated pattern
        System.out.println(pattern);

        // Appending the pattern to a StringBuffer
        StringBuffer sb = new StringBuffer();
        sb.append(pattern);

        // Printing the StringBuffer content
        System.out.println("StringBuffer content: " + sb.toString());
    }
}

Output:

--------------------------------------------------
StringBuffer content: --------------------------------------------------

In this example, String.repeat() is used to generate a line of dashes, and then the repeated string is appended to a StringBuffer, demonstrating how it can be useful for creating patterns or formatting output.

Conclusion

While the StringBuffer class does not have a repeat() method, the String.repeat() method introduced in Java 11 provides the functionality to repeat strings. You can use String.repeat() to generate a repeated string and then append it to a StringBuffer as needed. This method allows you to perform various string repetition operations, making it a versatile tool for string manipulation and pattern generation in various scenarios.

Comments