Concatenate Strings in Kotlin with the + Operator
In Kotlin, you can concatenate strings with the + operator. When both operands are strings, Kotlin creates a new string containing the characters from the left operand followed by the characters from the right operand.
For simple cases, + is direct and readable. Kotlin also provides string templates for inserting values into text, joinToString() for joining collections with a separator, and string builders for assembling text repeatedly.
Kotlin String Concatenation Syntax with +
The syntax to concatenate two strings is given below.
string_1 + string_2
The operator + concatenates the two operands string1 and string2 and returns the resulting string. You may store the result in a variable.
You can chain the concatenation operator to combine more than two strings in one expression.
string_1 + string_2 + string_3 + ... + stringN
Concatenate Two Kotlin Strings
In this example, we shall take two strings string1 and string2 ,and concatenate them using String Concatenation Operator.
Kotlin Program
/**
* Kotlin - String Concatenation
*/
fun main(args: Array<String>) {
//a string
var string1 = "https://"
//another string
var string2 = "www.tutorialkart.com"
//concatenate strings
var stringResult = string1 + string2
print(stringResult)
}
Output
[https://www.tutorialkart.com](https://www.tutorialkart.com)
The original strings are unaffected by this concatenation operation. Kotlin strings are immutable, so concatenation produces a new String rather than modifying either source string.
Concatenate Three or More Kotlin Strings
You can concatenate more than two strings in a single statement.
In this example, we shall take three strings and perform string concatenation on them. The process should be same for any number of string you would like concatenate.
Kotlin Program
/**
* Kotlin - String Concatenation
*/
fun main(args: Array<String>) {
//a string
var string1 = "https://"
//another string
var string2 = "www.tutorialkart.com"
//yet another string
var string3 = "/kotlin-tutorial/"
//concatenate strings
var stringResult = string1 + string2 + string3
print(stringResult)
}
Output
[https://www.tutorialkart.com/kotlin-tutorial/](https://www.tutorialkart.com/kotlin-tutorial/)
The order in which the strings are concatenated is determined by the order in which we provide the operands to the concatenation operator.
Kotlin String Templates for Concatenating Text and Values
When you are inserting variables or expressions into text, Kotlin string templates are often easier to read than a long chain of + operators. Prefix a variable with $, or place an expression inside ${...}.
fun main() {
val firstName = "Ravi"
val score = 92
val message = "$firstName scored $score marks."
val nextScore = "After bonus: ${score + 5}"
println(message)
println(nextScore)
}
Output
Ravi scored 92 marks.
After bonus: 97
Use $name for a simple variable reference. Use ${expression} when you need to access a property, call a function, or evaluate an expression inside the string.
Concatenate Kotlin Strings with Non-String Values
A string can also be concatenated with values such as integers or booleans when the string appears on the left side of +. Kotlin converts the value to its string representation as part of the concatenation.
fun main() {
val count = 3
val available = true
println("Items: " + count)
println("Available: " + available)
}
Output
Items: 3
Available: true
For mixed text and values, a string template such as "Items: $count" is usually more concise.
Join Kotlin Strings with a Separator Using joinToString()
If the strings are stored in a list or another iterable, use joinToString() when you want a separator between the values. This avoids manually inserting commas, spaces, slashes, or other separators with repeated + operations.
fun main() {
val words = listOf("Kotlin", "Java", "Swift")
val result = words.joinToString(", ")
println(result)
}
Output
Kotlin, Java, Swift
The separator is placed only between elements. For example, joinToString(" / ") joins the same list as Kotlin / Java / Swift.
Build Longer Kotlin Strings Repeatedly
Repeated concatenation inside a loop can create many intermediate strings because String values are immutable. When you are assembling a larger string step by step, Kotlin’s buildString function provides a convenient builder-based approach.
fun main() {
val result = buildString {
append("Name: ")
append("Ravi")
append(", Score: ")
append(92)
}
println(result)
}
Output
Name: Ravi, Score: 92
Choosing a Kotlin String Concatenation Method
- Use
+for a small number of simple string values. - Use string templates when inserting variables or expressions into readable text.
- Use
joinToString()when joining a collection of values with a separator. - Use
buildStringorStringBuilderwhen constructing text repeatedly or conditionally.
These approaches solve related but different problems. Choosing the one that matches the shape of the data usually makes the Kotlin code shorter and easier to maintain.
Kotlin String Concatenation Key Points
The + operator concatenates strings in left-to-right order and returns a new string. Kotlin string templates provide a cleaner form for embedding values, while joinToString() is useful for collections and builder-based APIs are better suited to repeated string construction.
In this Kotlin Tutorial, we learned how to concatenate strings in Kotlin using String concatenation operator.
TutorialKart.com