Kotlin String slice()

Kotlin String slice() Tutorial

The String.slice() function in Kotlin is used to create a new string containing a subsequence of characters from the original string. It takes a range or an iterable of indices as input and returns a substring consisting of the characters at the specified indices.

In this tutorial, we’ll explore the syntax of the slice() function and provide examples of its usage in Kotlin strings.

Syntax

The syntax of the slice() function is as follows:

fun String.slice(indices: IntRange): String
fun String.slice(indices: Iterable<Int>): String

The indices parameter can be either an IntRange or an Iterable of integers specifying the indices of the characters to include in the substring.

Examples for String slice() function

1. Slice given String with IntRange

In this example, we’ll use slice() to create a substring from a string using an IntRange of indices.

  1. Take a string value in originalString.
  2. Define an IntRange representing the indices of the characters to include in the substring.
  3. Call slice() function on originalString with the IntRange as an argument. The function returns a substring containing characters at the specified indices.
  4. You may print the original string and the resulting substring to the console output.

Kotlin Program

fun main() {
    val originalString = "Hello World!"

    // Defining an IntRange of indices (3 to 11)
    val indicesRange = 3..9

    // Using slice() to create a substring from the original string
    val resultSubstring = originalString.slice(indicesRange)

    // Printing the original string and the resulting substring
    println("Original String:\n$originalString\n")
    println("Result Substring:\n$resultSubstring")
}

Output

Original String:
Hello World!

Result Substring:
lo Worl

2. Slice given String with Iterable of Indices

In this example, we’ll use slice() to create a substring from a string using an iterable of indices.

  1. Take a string value in originalString.
  2. Define an iterable (e.g., a list) of integers representing the indices of the characters to include in the substring.
  3. Call slice() function on originalString with the iterable as an argument. The function returns a substring containing characters at the specified indices.
  4. You may print the original string and the resulting substring to the console output.

Kotlin Program

fun main() {
    val originalString = "Hello World!"

    // Defining an iterable of indices (5, 8, 12)
    val indicesList = listOf(1, 8, 9)

    // Using slice() to create a substring from the original string
    val resultSubstring = originalString.slice(indicesList)

    // Printing the original string and the resulting substring
    println("Original String:\n$originalString\n")
    println("Result Substring:\n$resultSubstring")
}

Output

Original String:
Hello World!

Result Substring:
erl

Summary

In this tutorial, we’ve covered the slice() function in Kotlin strings, its syntax, and how to use it to create substrings by specifying either an IntRange or an iterable of indices. This function provides flexibility in extracting portions of a string based on the desired character positions.