The substring function in Kotlin is used to extract a part of a string. This function is part of the Kotlin standard library and provides a straightforward way to get a substring from a given string.
Table of Contents
- Introduction
substringFunction Syntax- Understanding
substring - Examples
- Basic Usage
- Using
substringwith Start and End Index - Handling Index Out of Bounds
- Real-World Use Case
- Conclusion
Introduction
The substring function extracts a part of a string, specified by a range of indices. This is useful for tasks such as parsing, extracting, and manipulating portions of a string.
substring Function Syntax
The syntax for the substring function is as follows:
fun String.substring(startIndex: Int): String
fun String.substring(startIndex: Int, endIndex: Int): String
Parameters:
startIndex: The starting index (inclusive) of the substring.endIndex: The ending index (exclusive) of the substring (optional).
Returns:
- A new string containing the characters from the specified range of the original string.
Throws:
IndexOutOfBoundsExceptionif the start or end index is out of bounds.
Understanding substring
The substring function can be used in two ways:
- Extracting a substring from a starting index to the end of the string.
- Extracting a substring from a starting index to an ending index.
Examples
Basic Usage
To demonstrate the basic usage of substring, we will create a string and extract substrings from it.
Example
fun main() {
val text = "Hello, World!"
val subText1 = text.substring(7)
val subText2 = text.substring(0, 5)
println("Original text: $text")
println("Substring from index 7: $subText1")
println("Substring from index 0 to 5: $subText2")
}
Output:
Original text: Hello, World!
Substring from index 7: World!
Substring from index 0 to 5: Hello
Using substring with Start and End Index
This example shows how to use substring with both a start and end index to extract a specific portion of a string.
Example
fun main() {
val text = "Kotlin Programming"
val subText = text.substring(7, 18)
println("Original text: $text")
println("Substring from index 7 to 18: $subText")
}
Output:
Original text: Kotlin Programming
Substring from index 7 to 18: Programming
Handling Index Out of Bounds
This example demonstrates how to handle cases where the indices are out of bounds.
Example
fun main() {
val text = "Kotlin"
try {
val subText = text.substring(3, 10)
println("Substring from index 3 to 10: $subText")
} catch (e: IndexOutOfBoundsException) {
println("Error: ${e.message}")
}
}
Output:
Error: String index out of range: 10
Real-World Use Case
Extracting a Domain from an Email
In real-world applications, the substring function can be used to extract specific parts of a string, such as extracting the domain from an email address.
Example
fun main() {
val email = "user@example.com"
val atIndex = email.indexOf('@')
val domain = email.substring(atIndex + 1)
println("Email: $email")
println("Domain: $domain")
}
Output:
Email: user@example.com
Domain: example.com
Conclusion
The substring function in Kotlin is a convenient method for extracting parts of a string. It provides a simple way to manipulate and handle substrings based on specified indices. By understanding and using this function, you can effectively manage string extraction and manipulation operations in your Kotlin applications.