Kotlin String subSequence

The subSequence function in Kotlin is used to extract a subsequence of characters from a string, starting from a specified start index and ending at a specified end index. This function belongs to the String class in the Kotlin standard library and provides a straightforward way to obtain a part of a string.

Table of Contents

  1. Introduction
  2. subSequence Function Syntax
  3. Understanding subSequence
  4. Examples
    • Basic Usage
    • Extracting a Subsequence from the Middle
    • Using subSequence with Different Indices
  5. Real-World Use Case
  6. Conclusion

Introduction

The subSequence function extracts a subsequence of characters from a string, starting from the specified start index and ending at the specified end index. The result is a new CharSequence that represents the specified part of the original string.

subSequence Function Syntax

The syntax for the subSequence function is as follows:

fun CharSequence.subSequence(startIndex: Int, endIndex: Int): CharSequence

Parameters:

  • startIndex: The starting index (inclusive) of the subsequence.
  • endIndex: The ending index (exclusive) of the subsequence.

Returns:

  • A new CharSequence representing the specified part of the original string.

Throws:

  • IndexOutOfBoundsException if the start or end index is out of range.

Understanding subSequence

The subSequence function allows you to extract a part of a string by specifying the start and end indices. The start index is inclusive, and the end index is exclusive. The result is a new CharSequence that contains the characters from the specified range of the original string.

Examples

Basic Usage

To demonstrate the basic usage of subSequence, we will extract a subsequence from a string.

Example

fun main() {
    val text = "Hello, World!"
    val subSequence = text.subSequence(7, 12)
    println("Original text: $text")
    println("Subsequence: $subSequence")
}

Output:

Original text: Hello, World!
Subsequence: World

Extracting a Subsequence from the Middle

This example shows how to extract a subsequence from the middle of a string.

Example

fun main() {
    val text = "Kotlin Programming"
    val subSequence = text.subSequence(7, 18)
    println("Original text: $text")
    println("Subsequence: $subSequence")
}

Output:

Original text: Kotlin Programming
Subsequence: Programming

Using subSequence with Different Indices

This example demonstrates how to use subSequence with different start and end indices to extract various parts of a string.

Example

fun main() {
    val text = "Hello, Kotlin!"
    val subSequence1 = text.subSequence(0, 5)
    val subSequence2 = text.subSequence(7, 13)

    println("Original text: $text")
    println("Subsequence 1: $subSequence1")
    println("Subsequence 2: $subSequence2")
}

Output:

Original text: Hello, Kotlin!
Subsequence 1: Hello
Subsequence 2: Kotlin

Real-World Use Case

Extracting a Substring from User Input

In real-world applications, the subSequence function can be used to extract a specific part of user input, such as extracting a substring for validation or processing.

Example

fun main() {
    val userInput = "2024-07-01"
    val year = userInput.subSequence(0, 4)
    val month = userInput.subSequence(5, 7)
    val day = userInput.subSequence(8, 10)

    println("Original input: $userInput")
    println("Year: $year")
    println("Month: $month")
    println("Day: $day")
}

Output:

Original input: 2024-07-01
Year: 2024
Month: 07
Day: 01

Conclusion

The subSequence function in Kotlin's String class is a convenient method for extracting a subsequence of characters from a string. It provides a simple way to obtain a part of a string for various use cases, including validation, substring extraction, and data processing. By understanding and using this function, you can effectively manage string subsequences in your Kotlin applications.

Comments