🎓 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
String.repeat() method in Java is used to repeat a 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.Table of Contents
- Introduction
repeatMethod Syntax- Examples
- Repeating a String
- Handling Edge Cases
- Conclusion
Introduction
The String.repeat() method is a member of the String class in Java, introduced in Java 11. It allows you to create a new string that consists of the original string repeated a specified number of times.
repeat Method Syntax
The syntax for the repeat method is as follows:
public String repeat(int count)
- count: The number of times the string should be repeated.
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";
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
The repeat method handles edge cases such as repeating a string zero times or with a negative count.
Example
public class RepeatExample {
public static void main(String[] args) {
String str = "Hello";
String repeatedZeroTimes = str.repeat(0);
System.out.println("Repeated zero times: '" + repeatedZeroTimes + "'");
try {
String repeatedNegativeTimes = str.repeat(-1);
System.out.println("Repeated negative times: '" + repeatedNegativeTimes + "'");
} catch (IllegalArgumentException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Output:
Repeated zero times: ''
Error: count is negative: -1
Repeating an Empty String
Repeating an empty string will always result in an empty string, regardless of the count.
Example
public class RepeatExample {
public static void main(String[] args) {
String str = "";
String repeatedStr = str.repeat(5);
System.out.println("Repeated empty string: '" + repeatedStr + "'");
}
}
Output:
Repeated empty string: ''
Conclusion
The String.repeat() method in Java is used for repeating strings a specified number of times. By understanding how to use this method, you can efficiently create repeated strings in your Java applications. Whether you are repeating a string multiple times, handling edge cases, or dealing with empty strings, the repeat method provides a reliable solution for these tasks.
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