🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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
- Introduction
repeatMethod Syntax- Examples
- Repeating a String
- Handling Zero and Negative Repetitions
- Real-World Use Case
- Example: Generating a Pattern
- 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
counttimes.
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.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment