Check if a string ends with another string or character in Kotlin

How to check if a string ends with another string or character in Kotlin:

In this post, we will learn how to check if a string ends with another string or character. There is an inbuilt method called endsWith that can be used to check if a string ends with another string or character. This post will show you the definition of endsWith and how to use it with examples.

Definition of endsWith:

The endsWith method is defined as like below:

fun str.endsWith(
    suffix: String,
    ignoreCase: Boolean = false
): Boolean

This method returns one boolean value. It returns true if the string str ends with the suffix. Else, it returns false. The ignoreCase parameter is used to define if the character case should be ignored or not. It is an optional value and it will be false if we don’t pass it.

We can also use this method to check if a string ends with a specific character or not.

fun str.endsWith(
    char: Char,
    ignoreCase: Boolean = false
): Boolean

Let’s learn how to use this method with examples.

Example 1: How to use endsWith to check if a string ends with a substring:

Let’s learn how to use the endsWith method to check if a string ends with a specific substring or not:

fun main() {
    val strArray = arrayOf("Hello world", "hello World", "Hello Universe", "world", "   world")

    for (str in strArray) {
        if (str.endsWith("world")) {
            println("'$str' ends with 'world'")
        } else {
            println("'$str' doesn't end with 'world'")
        }

    }
}

In this example, the endsWith method checks if the strings in the array strArray ends with the string world or not. It prints one message based on the response of the endsWith method. It will print the below output:

'Hello world' ends with 'world'
'hello World' doesn't end with 'world'
'Hello Universe' doesn't end with 'world'
'world' ends with 'world'
'   world' ends with 'world'

Example 2: How to use endsWith to check if a string ends with a character:

Let’s try to use endsWith to check if a string ends with a character or not. Let’s take a look at the below example:

fun main() {
    val strArray = arrayOf("Hello world", "hello WorlD", "Hello Universe", "world ", "   worlD")

    for (str in strArray) {
        if (str.endsWith("d")) {
            println("'$str' ends with 'd'")
        } else {
            println("'$str' doesn't end with 'd'")
        }
        
    }
}

This program checks if the strings in the array strArray ends with the character d or not. If you run this program, it will print the below output:

'Hello world' ends with 'd'
'hello WorlD' doesn't end with 'd'
'Hello Universe' doesn't end with 'd'
'world ' doesn't end with 'd'
'   worlD' doesn't end with 'd'

Example 3: How to use endsWith to check if a string ends with a substring with ignoreCase value true:

Let’s try to use endsWith with ignoreCase as true. This will not consider the character case while checking for the last character.

fun main() {
    val strArray = arrayOf("Hello world", "hello WorlD", "Hello Universe", "world ", "   worlD")

    for (str in strArray) {
        if (str.endsWith("d", ignoreCase = true)) {
            println("'$str' ends with 'd'")
        } else {
            println("'$str' doesn't end with 'd'")
        }

    }
}

It will give: Kotlin endsWith example

As you can see here, it returns true for all strings ending with d or D.

You might also like: