Extend the functionality of the String class with a custom Kotlin extension function to check if a string is a palindrome.

fun String.isPalindrome(): Boolean {
    val cleanString = this.replace("\\s+".toRegex(), "").toLowerCase()
    val reverseString = cleanString.reversed()
    return cleanString == reverseString
}

Usage:

val text = "racecar"
val isPalindrome = text.isPalindrome()
Share this post