🎓 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 endsWith function in Kotlin is used to check if a string ends with a specified suffix. This function belongs to the String class in the Kotlin standard library and provides a straightforward way to perform suffix matching within a string.
Table of Contents
- Introduction
endsWithFunction Syntax- Understanding
endsWith - Examples
- Basic Usage
- Using
endsWithwith Ignore Case - Checking for Multiple Suffixes
- Real-World Use Case
- Conclusion
Introduction
The endsWith function checks if a string ends with a specified suffix. This is useful for various string operations such as validation, formatting, and file type checking.
endsWith Function Syntax
The syntax for the endsWith function is as follows:
fun String.endsWith(suffix: String, ignoreCase: Boolean = false): Boolean
fun String.endsWith(suffix: Char, ignoreCase: Boolean = false): Boolean
Parameters:
suffix: The suffix to check for at the end of the string.ignoreCase: If true, the search will ignore case differences (default is false).
Returns:
trueif the string ends with the specified suffix,falseotherwise.
Understanding endsWith
The endsWith function checks if the string ends with the given suffix. It can perform a case-sensitive or case-insensitive check based on the ignoreCase parameter.
Examples
Basic Usage
To demonstrate the basic usage of endsWith, we will create a string and check if it ends with a specific suffix.
Example
fun main() {
val text = "Hello, World!"
val endsWithExclamation = text.endsWith("!")
val endsWithWorld = text.endsWith("World!")
println("Does the text end with '!'? $endsWithExclamation")
println("Does the text end with 'World!'? $endsWithWorld")
}
Output:
Does the text end with '!'? true
Does the text end with 'World!'? true
Using endsWith with Ignore Case
This example shows how to use the ignoreCase parameter for case-insensitive suffix checking.
Example
fun main() {
val text = "Hello, World!"
val endsWithWorldIgnoreCase = text.endsWith("world!", ignoreCase = true)
println("Does the text end with 'world!' (ignore case)? $endsWithWorldIgnoreCase")
}
Output:
Does the text end with 'world!' (ignore case)? true
Checking for Multiple Suffixes
This example demonstrates how to check for multiple possible suffixes using a combination of endsWith and other functions.
Example
fun main() {
val fileName = "document.txt"
val endsWithTxt = fileName.endsWith(".txt")
val endsWithDoc = fileName.endsWith(".doc")
println("Does the file name end with '.txt'? $endsWithTxt")
println("Does the file name end with '.doc'? $endsWithDoc")
}
Output:
Does the file name end with '.txt'? true
Does the file name end with '.doc'? false
Real-World Use Case
Validating File Extensions
In real-world applications, the endsWith function can be used to validate file extensions, ensuring that files have the correct format.
Example
fun main() {
val fileName = "report.pdf"
if (fileName.endsWith(".pdf")) {
println("The file is a PDF document.")
} else if (fileName.endsWith(".docx")) {
println("The file is a Word document.")
} else {
println("Unknown file format.")
}
}
Output:
The file is a PDF document.
Conclusion
The endsWith function in Kotlin's String class is a convenient method for checking if a string ends with a specified suffix. It provides a simple way to perform suffix matching for various use cases, including validation, formatting, and file type checking.
By understanding and using this function, you can effectively manage suffix-matching operations 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