You already know what a String type in Kotlin is and how to do basic operations with strings. In this topic, we're going to discuss several methods of getting a new string from the original one.
Don't forget that strings are immutable, therefore, functions that change a string return a new string.
Getting a part of a string
Oftentimes, you need to get only a certain part of a string. Kotlin provides the substring function for this.
The substring function accepts startIndex (inclusive) and lastIndex (exclusive) as arguments and returns a string that starts at the startIndex and ends right before the lastIndex.
val greeting = "Hello"
println(greeting.substring(0, 3)) // "Hel"
println(greeting.substring(1, 3)) // "el"
println(greeting.substring(2)) // "llo"
println(greeting.substring(4, 5)) // "o"The parameter lastIndex can be omitted; then you will get a substring from the startIndex element to the end of the original string.
The expression greeting.substring(0, 3) returns "Hel" instead of "Hell" because substring returns characters from the one at startIndex to the one right before lastIndex.
Don't forget that indexes in strings have to start with 0; otherwise, your program may crash. Also, keep it in mind that lastIndex must not be greater than the string length.
The substring method is not the only way to get part of a string. You can also use the substringAfter and substringBefore functions:
// greeting = "Hello"
println(greeting.substringAfter('l')) // "lo"
println(greeting.substringBefore('o')) // "Hell"
println(greeting.substringBefore('z')) // "Hello"These functions accept delimiter as an argument and return a string before/after the first occurrence of a specified delimiter. If the string does not contain any occurrences of the delimiter argument, the function returns the whole string.
Most of the functions in this topic accept not only characters but also strings as arguments.
Just keep it in mind that as a second argument, you can specify the message to be returned if the delimiter argument is not present in the string. For example, .substringBefore('z', "can't find a character").
The functions substringBeforeLast and substringAfterLast have a logic similar to substringBefore and substringAfter but return a string before or after the last occurrence of the delimiter.
// greeting = "Hello"
println(greeting.substringAfterLast('l')) // "o"
println(greeting.substringBeforeLast('l')) // "Hel"Replacing parts of a string
You've probably been in a situation when you need to replace just one word in a string. The replace function replaces all occurrences of the first argument in the string with the second argument.
val example = "Good morning..."
println(example.replace("morning", "bye")) // "Good bye..."
println(example.replace('.', '!')) // "Good morning!!!"As you know, strings are immutable, so the replace function returns a new string without changing the original string. So, if you run this code:
val example = "Good morning"
example.replace("morning", "bye")
println(example)It still prints Good morning.
If you need to replace only the first occurrence of an argument, use replaceFirst.
val example = "one one two three"
println(example.replaceFirst("one", "two")) // "two one two three"Changing the case
What if you need to replace all capital letters in a word with lowercase letters? Try to imagine how you would do it with the replace function. Now, that's too much code, right? Fortunately, in Kotlin you can use the lowercase function:
val example = "UPPERCASE String"
println(example.lowercase()) // uppercase stringIf you need to do the opposite, then use the uppercase function:
val example = "Lowercase String"
println(example.uppercase()) // LOWERCASE STRINGConclusion
In this topic, we've considered a few functions that produce a new string from the original one:
A substring from the original string;
The original string with replaced words or characters;
The original string in the lower or upper case.