Skip to main content

How to extract a substring from a string in Kotlin

How to extract a substring from a string in Kotlin.

Here's a step-by-step tutorial on how to extract a substring from a string in Kotlin:

  1. Using the substring() function:

    The substring() function is a built-in function in Kotlin that allows you to extract a substring from a string based on the specified indices.

    val originalString = "Hello, World!"
    val substring = originalString.substring(7, 12)
    println(substring) // Output: World

    In the example above, we have a string originalString with the value "Hello, World!". We use the substring() function to extract the substring starting at index 7 (inclusive) and ending at index 12 (exclusive). The resulting substring is "World", which is then printed to the console.

  2. Using the indexing operator:

    Kotlin supports indexing operator [], which allows you to access individual characters of a string by their index. You can use this operator to extract a substring by iterating over the desired indices.

    val originalString = "Hello, World!"
    val startIndex = 7
    val endIndex = 12

    val substring = buildString {
    for (i in startIndex until endIndex) {
    append(originalString[i])
    }
    }

    println(substring) // Output: World

    In this example, we define the start and end indices of the substring we want to extract. We then use the buildString function to construct the substring character by character using a for loop. The resulting substring is "World", which is printed to the console.

  3. Using regex pattern matching:

    Kotlin provides support for regular expressions, which can be used to extract substrings based on specific patterns. You can use the Regex class to define a pattern and then use the find() function to search for matches within the original string.

    val originalString = "Hello, World!"
    val pattern = Regex("[a-zA-Z]+")
    val matchResult = pattern.find(originalString)
    val substring = matchResult?.value

    println(substring) // Output: Hello

    In this example, we define a pattern using the Regex class, which matches one or more alphabetic characters. We then use the find() function to search for the first occurrence of this pattern within the original string. The resulting MatchResult object contains the matched substring, which is then printed to the console.

  4. Using the split() function:

    If you want to extract a substring based on a delimiter or separator, you can use the split() function. This function splits the original string into multiple substrings based on the specified delimiter and returns them as an array. You can then access the desired substring by its index.

    val originalString = "Hello, World!"
    val delimiter = ","
    val substrings = originalString.split(delimiter)
    val substring = substrings[1].trim()

    println(substring) // Output: World

    In this example, we split the original string into substrings based on the comma delimiter. The resulting array substrings contains two elements: "Hello" and " World!". We access the second element using index 1 and trim any leading or trailing whitespace using the trim() function. The resulting substring is "World", which is printed to the console.

These are some of the common ways to extract substrings from a string in Kotlin. Depending on your specific requirements, you can choose the approach that best suits your needs.