Strings in Java and Kotlin
This guide contains examples of how to perform typical tasks with strings in Java and Kotlin. It will help you migrate from Java to Kotlin and write your code in the authentically Kotlin way.
Concatenate strings
In Java, you can do this in the following way:
In Kotlin, use the dollar sign ($
) before the variable name to interpolate the value of this variable into your string:
You can interpolate the value of a complicated expression by surrounding it with curly braces, like in ${name.length}
. See string templates for more information.
Build a string
In Java, you can use the StringBuilder:
In Kotlin, use buildString() – an inline function that takes logic to construct a string as a lambda argument:
Under the hood, the buildString
uses the same StringBuilder
class as in Java, and you access it via an implicit this
inside the lambda.
Learn more about lambda coding conventions.
Create a string from collection items
In Java, you use the Stream API to filter, map, and then collect the items:
In Kotlin, use the joinToString() function, which Kotlin defines for every List:
Learn more about joinToString() usage.
Set default value if the string is blank
In Java, you can use the ternary operator:
Kotlin provides the inline function ifBlank() that accepts the default value as an argument:
Replace characters at the beginning and end of a string
In Java, you can use the replaceAll() function. The replaceAll()
function in this case accepts regular expressions ^##
and ##$
, which define strings starting and ending with ##
respectively:
In Kotlin, use the removeSurrounding() function with the string delimiter ##
:
Replace occurrences
In Java, you can use the Pattern and the Matcher classes, for example, to obfuscate some data:
In Kotlin, you use the Regex class that simplifies working with regular expressions. Additionally, use multiline strings to simplify a regex pattern by reducing the count of backslashes:
Split a string
In Java, to split a string with the period character (.
), you need to use shielding (\\
). This happens because the split() function of the String
class accepts a regular expression as an argument:
In Kotlin, use the Kotlin function split(), which accepts varargs of delimiters as input parameters:
If you need to split with a regular expression, use the overloaded split()
version that accepts the Regex
as a parameter.
Take a substring
In Java, you can use the substring() function, which accepts an inclusive beginning index of a character to start taking the substring from. To take a substring after this character, you need to increment the index:
In Kotlin, you use the substringAfter() function and don't need to calculate the index of the character you want to take a substring after:
Additionally, you can take a substring after the last occurrence of a character:
Use multiline strings
Before Java 15, there were several ways to create a multiline string. For example, using the join() function of the String
class:
In Java 15, text blocks appeared. There is one thing to keep in mind: if you print a multiline string and the triple-quote is on the next line, there will be an extra empty line:
The output:
If you put the triple-quote on the same line as the last word, this difference in behavior disappears.
In Kotlin, you can format your line with the quotes on the new line, and there will be no extra empty line in the output. The left-most character of any line identifies the beginning of the line. The difference with Java is that Java automatically trims indents, and in Kotlin you should do it explicitly:
The output:
To have an extra empty line, you should add this empty line to your multiline string explicitly.
In Kotlin, you can also use the trimMargin() function to customize the indents:
Learn more about multiline strings.
What's next?
Look through other Kotlin idioms.
Learn how to convert existing Java code to Kotlin with the Java to Kotlin converter.
If you have a favorite idiom, we invite you to share it by sending a pull request.