🎓 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 contains function in Kotlin is used to check if a string contains a specified substring or character. This function belongs to the String class in the Kotlin standard library and provides a straightforward way to perform substring search operations within a string.
Table of Contents
- Introduction
containsFunction Syntax- Understanding
contains - Examples
- Basic Usage
- Using
containswith Ignore Case - Checking for Characters
- Real-World Use Case
- Conclusion
Introduction
The contains function checks if a string contains a specified substring or character. This is useful for various string operations such as validation, search, and filtering.
contains Function Syntax
The syntax for the contains function is as follows:
fun String.contains(other: CharSequence, ignoreCase: Boolean = false): Boolean
fun String.contains(char: Char, ignoreCase: Boolean = false): Boolean
Parameters:
other: The substring to search for.char: The character to search for.ignoreCase: If true, the search will ignore case differences (default is false).
Returns:
trueif the string contains the specified substring or character,falseotherwise.
Understanding contains
The contains function searches for the specified substring or character within the string. If the substring or character is found, it returns true; otherwise, it returns false. The ignoreCase parameter allows for case-insensitive searches.
Examples
Basic Usage
To demonstrate the basic usage of contains, we will create a string and check if it contains a specific substring.
Example
fun main() {
val text = "Hello, World!"
val containsHello = text.contains("Hello")
val containsKotlin = text.contains("Kotlin")
println("Does the text contain 'Hello'? $containsHello")
println("Does the text contain 'Kotlin'? $containsKotlin")
}
Output:
Does the text contain 'Hello'? true
Does the text contain 'Kotlin'? false
Using contains with Ignore Case
This example shows how to use contains with the ignoreCase parameter for case-insensitive searches.
Example
fun main() {
val text = "Hello, World!"
val containsHelloCaseInsensitive = text.contains("hello", ignoreCase = true)
val containsWorldCaseSensitive = text.contains("world", ignoreCase = false)
println("Does the text contain 'hello' (ignore case)? $containsHelloCaseInsensitive")
println("Does the text contain 'world' (case sensitive)? $containsWorldCaseSensitive")
}
Output:
Does the text contain 'hello' (ignore case)? true
Does the text contain 'world' (case sensitive)? false
Checking for Characters
This example demonstrates how to use contains to check for the presence of a specific character.
Example
fun main() {
val text = "Kotlin Programming"
val containsK = text.contains('K')
val containsZ = text.contains('Z')
println("Does the text contain 'K'? $containsK")
println("Does the text contain 'Z'? $containsZ")
}
Output:
Does the text contain 'K'? true
Does the text contain 'Z'? false
Real-World Use Case
Validating User Input
In real-world applications, the contains function can be used to validate user input, such as checking if an email address contains the '@' symbol.
Example
fun main() {
val email = "user@example.com"
if (email.contains('@')) {
println("Valid email address.")
} else {
println("Invalid email address.")
}
}
Output:
Valid email address.
Conclusion
The contains function in Kotlin's String class is a convenient method for checking if a string contains a specified substring or character. It provides a simple way to perform search operations within strings and can be used for various applications such as validation and filtering. By understanding and using this function, you can effectively manage substring searches 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