The Kotlin String class in the kotlin-stdlib/kotlin package provides a variety of functions to handle strings. Strings in Kotlin are used to store sequences of characters, and Kotlin provides many utility functions to manipulate strings easily and efficiently.
This guide covers various methods available in the Kotlin String class. Each method is described in simple terms to help beginners understand how to use them. These methods enable you to perform common operations such as querying, modifying, and transforming strings.
Kotlin String Functions
The table below contains various functions of the Kotlin String class, each with a link to a detailed explanation, examples, and real-world uses. Click on the method names to learn more about how to use them effectively in your applications.
| Method | Description |
|---|---|
| Kotlin String length Property | Returns the length of the string. |
| Kotlin String get Function | Returns the character at the specified index. |
| Kotlin String isEmpty Function | Checks if the string is empty. |
| Kotlin String isNotEmpty Function | Checks if the string is not empty. |
| Kotlin String isBlank Function | Checks if the string is blank. |
| Kotlin String isNotBlank Function | Checks if the string is not blank. |
| Kotlin String substring Function | Returns a substring of the string starting from the specified index and extending to the end of the string. |
| Kotlin String split Function | Splits the string into a list of substrings around occurrences of the specified delimiter. |
| Kotlin String trim Function | Removes leading and trailing whitespace from the string. |
| Kotlin String trimStart Function | Removes leading whitespace from the string. |
| Kotlin String trimEnd Function | Removes trailing whitespace from the string. |
| Kotlin String contains Function | Checks if the string contains the specified substring. |
| Kotlin String replace Function | Returns a new string with all occurrences of the specified old value replaced with the specified new value. |
| Kotlin String replaceFirst Function | Returns a new string with the first occurrence of the specified old value replaced with the specified new value. |
| Kotlin String endsWith Function | Checks if the string ends with the specified suffix. |
| Kotlin String capitalize Function | Returns a copy of the string with the first letter capitalized. |
| Kotlin String decapitalize Function | Returns a copy of the string with the first letter decapitalized. |
| Kotlin String toCharArray Function | Returns an array of characters representing the string. |
| Kotlin String toByteArray Function | Encodes the string into a sequence of bytes using the specified charset and returns the result as a byte array. |
| Kotlin String plus Function | Concatenates the string with the specified string and returns the result. |
| Kotlin String compareTo Function | Compares two strings lexicographically. |
| Kotlin String repeat Function | Returns a string obtained by repeating the original string the specified number of times. |
| Kotlin String indexOf Function | Returns the index within the string of the first occurrence of the specified substring. |
| Kotlin String lastIndexOf Function | Returns the index within the string of the last occurrence of the specified substring. |
| Kotlin String indexOfFirst Function | Returns the index of the first character matching the given predicate, or -1 if the string does not contain such a character. |
| Kotlin String indexOfLast Function | Returns the index of the last character matching the given predicate, or -1 if the string does not contain such a character. |
| Kotlin String subSequence Function | Returns a new character sequence that is a subsequence of the string. |
| Kotlin String getOrNull Function | Returns the character at the specified index, or null if the index is out of bounds of the string. |
| Kotlin String getOrElse Function | Returns the character at the specified index, or the result of calling the specified function if the index is out of bounds of the string. |
| Kotlin String matches Function | Returns true if the string matches the specified regular expression. |
| Kotlin String toRegex Function | Returns a regular expression that matches the string as a literal. |
| Kotlin String sorted Function | Returns a list of all characters in the string, sorted in ascending order. |
| Kotlin String map Function | Returns a list of all characters in the string transformed by the specified function. |
| Kotlin String forEach Function | Performs the given action on each character in the string. |
| Kotlin String filter Function | Returns a string containing only characters matching the given predicate. |
| Kotlin String sortDescending Function | Returns a list of all characters in the string, sorted in descending order. |
| Kotlin String reduce Function | Accumulates value starting with the first element and applying operation from left to right to current accumulator value and each element. |
| Kotlin String fold Function | Accumulates value starting with the first element and applying operation from left to right to current accumulator value and each element, starting with the initial value. |
The Kotlin String class is part of the kotlin package and provides numerous utility functions to perform common operations on strings. Whether you need to query, modify, or transform strings, these functions make string manipulation in Kotlin both easy and efficient. For more detailed information, refer to the Kotlin String Documentation.