Skip to main content

How to check if a string starts or ends with a specific substring in Kotlin

How to check if a string starts or ends with a specific substring in Kotlin.

Here's a step-by-step tutorial on how to check if a string starts or ends with a specific substring in Kotlin.

Checking if a String Starts with a Specific Substring

To check if a string starts with a specific substring in Kotlin, you can use the startsWith() function. This function returns true if the string starts with the specified substring, and false otherwise.

Here's an example usage of the startsWith() function:

val str = "Hello, World!"
val prefix = "Hello"

if (str.startsWith(prefix)) {
println("The string starts with '$prefix'")
} else {
println("The string does not start with '$prefix'")
}

In this example, the startsWith() function is used to check if the string str starts with the substring prefix. If it does, the message "The string starts with 'Hello'" is printed; otherwise, the message "The string does not start with 'Hello'" is printed.

Checking if a String Ends with a Specific Substring

To check if a string ends with a specific substring in Kotlin, you can use the endsWith() function. This function returns true if the string ends with the specified substring, and false otherwise.

Here's an example usage of the endsWith() function:

val str = "Hello, World!"
val suffix = "World!"

if (str.endsWith(suffix)) {
println("The string ends with '$suffix'")
} else {
println("The string does not end with '$suffix'")
}

In this example, the endsWith() function is used to check if the string str ends with the substring suffix. If it does, the message "The string ends with 'World!'" is printed; otherwise, the message "The string does not end with 'World!'" is printed.

Case-Insensitive Comparison

By default, the startsWith() and endsWith() functions perform case-sensitive comparisons. If you want to perform a case-insensitive comparison, you can use the startsWith() and endsWith() functions with the ignoreCase parameter set to true.

Here's an example usage of case-insensitive comparisons:

val str = "Hello, World!"
val prefix = "hello"

if (str.startsWith(prefix, ignoreCase = true)) {
println("The string starts with '$prefix' (case-insensitive)")
} else {
println("The string does not start with '$prefix' (case-insensitive)")
}

In this example, the startsWith() function is used with the ignoreCase parameter set to true, which performs a case-insensitive comparison. If the string str starts with the substring prefix (ignoring the case), the message "The string starts with 'hello' (case-insensitive)" is printed; otherwise, the message "The string does not start with 'hello' (case-insensitive)" is printed.

Similarly, you can use the endsWith() function with the ignoreCase parameter to perform a case-insensitive comparison when checking if a string ends with a specific substring.

That's it! You now know how to check if a string starts or ends with a specific substring in Kotlin.