Kotlin String find() Tutorial
The String.find()
function in Kotlin is used to find the first character in the string that matches a given predicate. It returns the first character that satisfies the specified condition, or null
if no such character is found.
In this tutorial, we’ll explore the syntax of the find()
function and provide examples of its usage in Kotlin strings.
Syntax
The syntax of the find()
function is as follows:
fun CharSequence.find(
predicate: (Char) -> Boolean
): Char?
where
Parameter | Description |
---|---|
predicate | A function that defines the condition for selecting a character. It takes a character as an argument and returns true if the character satisfies the condition, and false otherwise. |
The predicate
parameter is a function that determines whether a character should be selected. The function returns true
for characters that meet the specified condition and false
for those that do not. The first character that satisfies the condition is then returned by the find()
function.
Examples for String find() function
1. Find the First Uppercase Letter in given String
In this example, we’ll use find()
to find the first uppercase letter in a given string.
- Take a string value in
text
. - Define a predicate function that returns
true
for uppercase letters andfalse
for other characters. - Call
find()
function ontext
with the predicate function as an argument. The function returns the first uppercase letter in the string, ornull
if none is found. - You may print the resulting character to the console output.
Kotlin Program
fun main() {
val text = "hello World"
// Defining a predicate to select uppercase letters
val predicate: (Char) -> Boolean = { it.isUpperCase() }
// Using find() to find the first uppercase letter in the string
val result = text.find(predicate)
// Printing the original string and the resulting character
println("Original String:\n$text\n")
println("First Uppercase Letter: $result")
}
Output
Original String:
hello World
First Uppercase Letter: W
2. Find the First Digit in given String
In this example, we’ll use find()
to find the first digit in a given string.
- Take a string value in
text
. - Define a predicate function that returns
true
for digits andfalse
for other characters. - Call
find()
function ontext
with the predicate function as an argument. The function returns the first digit in the string, ornull
if none is found. - You may print the resulting character to the console output.
Kotlin Program
fun main() {
val text = "hello world 54321"
// Defining a predicate to select digits
val predicate: (Char) -> Boolean = { it.isDigit() }
// Using find() to find the first digit in the string
val result = text.find(predicate)
// Printing the original string and the resulting character
println("Original String:\n$text\n")
println("First Digit: $result")
}
Output
Original String:
hello world 54321
First Digit: 5
Summary
In this tutorial, we’ve covered the find()
function in Kotlin strings, its syntax, and how to use it to find the first character that satisfies a specified condition in a string. This function is useful for extracting specific elements from a string based on a given predicate.