Kotlin String findLast() Tutorial
The String.findLast()
function in Kotlin is used to find the last character in the string that matches a given predicate. It returns the last character that satisfies the specified condition, or null
if no such character is found.
In this tutorial, we’ll explore the syntax of the findLast()
function and provide examples of its usage in Kotlin strings.
Syntax
The syntax of the findLast()
function is as follows:
inline fun CharSequence.findLast(
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 last character that satisfies the condition is then returned by the findLast()
function.
Examples for String findLast() function
1. Find the Last Uppercase Letter in the given String
In this example, we’ll use findLast()
to find the last 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
findLast()
function ontext
with the predicate function as an argument. The function returns the last 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 Banana"
// Defining a predicate to select uppercase letters
val predicate: (Char) -> Boolean = { it.isUpperCase() }
// Using findLast() to find the last uppercase letter in the string
val result = text.findLast(predicate)
// Printing the original string and the resulting character
println("Original String:\n$text\n")
println("Last Uppercase Letter: $result")
}
Output
Original String:
Hello World Banana
Last Uppercase Letter: B
2. Find the Last Digit in the given String
In this example, we’ll use findLast()
to find the last 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
findLast()
function ontext
with the predicate function as an argument. The function returns the last 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 = "12345 hello world"
// Defining a predicate to select digits
val predicate: (Char) -> Boolean = { it.isDigit() }
// Using findLast() to find the last digit in the string
val result = text.findLast(predicate)
// Printing the original string and the resulting character
println("Original String:\n$text\n")
println("Last Digit: $result")
}
Output
Original String:
12345 hello world
Last Digit: 5
Summary
In this tutorial, we’ve covered the findLast()
function in Kotlin strings, its syntax, and how to use it to find the last 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.