🎓 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
The repeat function in Kotlin is used to create a new string by repeating the original string a specified number of times. This function belongs to the String class in the Kotlin standard library and provides a straightforward way to repeat a string.
Table of Contents
- Introduction
repeatFunction Syntax- Understanding
repeat - Examples
- Basic Usage
- Using
repeatwith Different Counts
- Real-World Use Case
- Conclusion
Introduction
The repeat function generates a new string by repeating the original string a specified number of times. This is useful for tasks such as creating patterns, generating repeated content, and formatting output.
repeat Function Syntax
The syntax for the repeat function is as follows:
fun String.repeat(n: Int): String
Parameters:
n: The number of times to repeat the original string. This must be a non-negative integer.
Returns:
- A new string that consists of the original string repeated
ntimes.
Throws:
IllegalArgumentExceptionifnis negative.
Understanding repeat
The repeat function concatenates the original string with itself n times to create a new string. If n is 0, the function returns an empty string.
Examples
Basic Usage
To demonstrate the basic usage of repeat, we will create a string and repeat it a specified number of times.
Example
fun main() {
val text = "Hello"
val repeatedText = text.repeat(3)
println("Original text: $text")
println("Repeated text: $repeatedText")
}
Output:
Original text: Hello
Repeated text: HelloHelloHello
Using repeat with Different Counts
This example shows how to use repeat with different repeat counts.
Example
fun main() {
val text = "Kotlin"
val repeatedOnce = text.repeat(1)
val repeatedTwice = text.repeat(2)
val repeatedZeroTimes = text.repeat(0)
println("Original text: $text")
println("Repeated once: $repeatedOnce")
println("Repeated twice: $repeatedTwice")
println("Repeated zero times: '$repeatedZeroTimes'")
}
Output:
Original text: Kotlin
Repeated once: Kotlin
Repeated twice: KotlinKotlin
Repeated zero times: ''
Real-World Use Case
Creating a Divider Line
In real-world applications, the repeat function can be used to create repeated patterns, such as a divider line made of hyphens.
Example
fun main() {
val line = "-".repeat(30)
println(line)
println("Title")
println(line)
}
Output:
------------------------------
Title
------------------------------
Conclusion
The repeat function in Kotlin's String class is a convenient method for creating a new string by repeating the original string a specified number of times. It provides a simple way to generate repeated content for various use cases, including formatting, pattern creation, and generating repeated content.
By understanding and using this function, you can effectively manage string repetition in your Kotlin applications.
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