Check if a string starts with a character or sub-string in Kotlin

How to check if a string starts with a specific character or a sub-string in Kotlin:

In this post, we will learn how to check if a string starts with a specific character or a sub-string in Kotlin. There is an inbuilt method called startsWith that we can use to check if a string starts with any other character or sub-string.

Let me show you the definition of startsWith first.

Definition of startsWith method:

The startsWith method is used to check if a string or a character sequence starts with a specific character or any other character sequence. This method is defined as like below:

str.startsWith(
    prefix: String,
    startIndex: Int,
    ignoreCase: Boolean = false
): Boolean

It will check if the string str starts with prefix or not. The ignoreCase is a boolean value to define if character case should be ignore, it is false by default.

The startIndex is an optional value to define the starting index to start the searching.

It returns one boolean value. It returns true if the string str starts with prefix, else it returns false.

This method is also available for characters:

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

Example 1: Example of startsWith with substring to check if a string starts with a substring:

Let’s learn how to use startsWith to check if a string starts with another substring:

fun main() {
    val strArray = arrayOf("hello", "hello world", "xhello world", "123hello", " hello", "helllo")

    for (str in strArray) {
        if (str.startsWith("hello")) {
            println("'$str' starts with 'hello'")
        } else {
            println("'$str' doesn't start with 'hello'")
        }

    }
}

In this program,

  • The strArray is an array of strings.
  • The for in loop is iterating through the strings of the array one by one.
  • In the loop, it uses startsWith to check if the current string starts with ‘hello’ or not. It prints one message based on its return value.

If you run this program, it will print the below output:

'hello' starts with 'hello'
'hello world' starts with 'hello'
'xhello world' doesn't start with 'hello'
'123hello' doesn't start with 'hello'
' hello' doesn't start with 'hello'
'helllo' doesn't start with 'hello'

Similar to the above program, we can also check if a string starts with a specific character or not by using the startsWith method. Let me show you how it works.

Example 2: Example of startsWith to find if a string starts with a specific character:

Let’s check how to use the startsWith method to find if a string starts with a specific character:

fun main() {
    val strArray = arrayOf("hello", "Hello world", "xhello world", "123hello", " Hello", "HHelllo")

    for (str in strArray) {
        if (str.startsWith("H")) {
            println("'$str' starts with 'H'")
        } else {
            println("'$str' doesn't start with 'H'")
        }

    }
}

This program is checking if the strings starts with H. If you run this program, it will print the below output:

'hello' doesn't start with 'H'
'Hello world' starts with 'H'
'xhello world' doesn't start with 'H'
'123hello' doesn't start with 'H'
' Hello' doesn't start with 'H'
'HHelllo' starts with 'H'

Example 3: Example of startsWith with startIndex:

We can also use the startsWith method with a startIndex value. This will start the search from the specified index.

fun main() {
    val strArray = arrayOf("abc", "aabc", "1abc", "12abc", " abc")

    for (str in strArray) {
        if (str.startsWith("a", startIndex = 1)) {
            println("'$str' starts with 'a' from index 1")
        } else {
            println("'$str' doesn't start with 'a' from index 1")
        }

    }
}

This program is checking if the strings of the array strArray are starting with a from index 1. It will print:

'abc' doesn't start with 'a' from index 1
'aabc' starts with 'a' from index 1
'1abc' starts with 'a' from index 1
'12abc' doesn't start with 'a' from index 1
' abc' starts with 'a' from index 1

Example 3: Example of startsWith with ignoreCase:

If we pass true to the ignoreCase parameter, it will ignore the case, i.e. it will check for both upper case and lower case values:

fun main() {
    val strArray = arrayOf("HELlO", "hello World", "HEllo 123", " hello", "xyzHello")

    for (str in strArray) {
        if (str.startsWith("hello", ignoreCase = true)) {
            println("'$str' starts with 'hello'")
        } else {
            println("'$str' doesn't start with 'hello'")
        }

    }
}

This is similar to the above example. It is checking if the strings of the array strArray are starting with hello. We are also passing ignoreCase as true, so it will not consider the character case while it looks for the substring. It will print:

Kotlin example check string starts with another string or character

You might also like: