The StringBuilder.repeat()
method was introduced in Java 21 and is used to repeat a specified character or CharSequence
multiple times within a StringBuilder
object.
Table of Contents
- Introduction
repeat
Method Syntax- Examples
- Repeating a Code Point
- Repeating a CharSequence
- Real-World Use Case
- Conclusion
Introduction
The StringBuilder.repeat()
method allows you to repeat a specified character (represented as a Unicode code point) or a CharSequence
multiple times and append the result to the StringBuilder
object. This is useful for generating repeated patterns, such as padding strings or creating repeated sequences of characters.
repeat() Method Syntax
The StringBuilder
class provides two overloaded repeat
methods:
repeat(int codePoint, int count)
repeat(CharSequence cs, int count)
Method 1: repeat(int codePoint, int count)
The syntax for repeating a code point is as follows:
public StringBuilder repeat(int codePoint, int count)
- codePoint: The Unicode code point of the character to be repeated.
- count: The number of times to repeat the character.
Method 2: repeat(CharSequence cs, int count)
The syntax for repeating a CharSequence
is as follows:
public StringBuilder repeat(CharSequence cs, int count)
- cs: The
CharSequence
to be repeated. - count: The number of times to repeat the
CharSequence
.
Examples
Repeating a Code Point
The repeat(int codePoint, int count)
method can be used to repeat a character represented by its Unicode code point multiple times.
Example
public class StringBuilderRepeatExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
// Repeat the character 'A' (Unicode code point 65) 5 times
sb.repeat(65, 5);
// Print the result
System.out.println("StringBuilder after repeating code point: " + sb.toString());
}
}
Output:
StringBuilder after repeating code point: AAAAA
Repeating a CharSequence
The repeat(CharSequence cs, int count)
method can be used to repeat a CharSequence
multiple times.
Example
public class StringBuilderRepeatExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
// Repeat the CharSequence "Java" 3 times
sb.repeat("Java", 3);
// Print the result
System.out.println("StringBuilder after repeating CharSequence: " + sb.toString());
}
}
Output:
StringBuilder after repeating CharSequence: JavaJavaJava
Real-World Use Case
Example: Creating a Repeated Pattern for Formatting
In a real-world scenario, you might need to create a repeated pattern for formatting purposes, such as generating a separator line in a text file or console output.
Example Code
public class PatternGenerator {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
// Generate a repeated pattern of 50 dashes
sb.repeat('-', 50);
// Print the repeated pattern
System.out.println(sb.toString());
}
}
Output:
--------------------------------------------------
Example: Padding a String
Another use case could be padding a string with repeated characters to ensure it meets a certain length.
Example Code
public class StringPadding {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Data");
// Calculate how many spaces are needed to pad to 10 characters
int paddingCount = 10 - sb.length();
// Append the spaces to the StringBuilder
sb.repeat(' ', paddingCount);
// Print the padded string
System.out.println("Padded string: '" + sb.toString() + "'");
}
}
Output:
Padded string: 'Data '
Conclusion
The StringBuilder.repeat()
method introduced in Java 21 is used for repeating characters or CharSequence
multiple times within a StringBuilder
object. By understanding how to use these overloaded methods, you can efficiently generate repeated patterns and handle various scenarios where duplicating a sequence of characters is required. Whether you need to repeat a single character, a string, or create padding for formatting purposes, the repeat
method provides a versatile solution for these tasks.
Comments
Post a Comment
Leave Comment