Quantcast
Channel: Baeldung
Viewing all articles
Browse latest Browse all 4536

Concatenate Strings in Kotlin

$
0
0

1. Introduction

In this short tutorial, we’ll investigate different ways of concatenating strings in Kotlin.

2. Using the plus() Method

Kotlin’s String class contains a plus() method:

operator fun plus(other: Any?): String (source)

It returns a String obtained by concatenating reference String with the String passed as an argument.

For example:

@Test
fun givenTwoStrings_concatenateWithPlusMethod_thenEquals() {
    val a = "Hello"
    val b = "Baeldung"
    val c = a.plus(" ").plus(b)

    assertEquals("Hello Baeldung", c)
}

Also, it is important to realize that if the object passed in isn’t a String, the String representation of the object will be used.

3. Using the + Operator

The simplest way of concatenating Strings in Kotlin is to use the + operator. As a result, we get a new String object composed of Strings on the left and the right side of the operator:

@Test
fun givenTwoStrings_concatenateWithPlusOperator_thenEquals() {
    val a = "Hello"
    val b = "Baeldung"
    val c = a + " " + b

    assertEquals("Hello Baeldung", c)
}

Another key point is that in Kotlin, thanks to operator overload, the + operator gets resolved to the plus() method.

In general, this is a common method for concatenating small numbers of Strings.

4. Using StringBuilder

As we know, String objects are immutable. With each concatenation using the + operator or plus() method, we get a new String object. In contrast, to avoid unnecessary String object creation, we can use a StringBuilder.

Hence, StringBuilder creates a single internal buffer that contains the final string.

Therefore, StringBuilder is more efficient when concatenating a large number of strings.

Here is a String concatenation example using StringBuilder:

@Test
fun givenTwoStrings_concatenateWithStringBuilder_thenEquals() {
    val builder = StringBuilder()
    builder.append("Hello")
           .append(" ")
           .append("Baeldung")

    assertEquals("Hello Baeldung", builder.toString())
}

Finally, we can use StringBuffer for thread-safe concatenation instead of StringBuilder.

5. Using String Templates

Kotlin also has a feature called String templates. String templates contain expressions that get evaluated to build a String.

String template expressions start with a dollar sign followed by variable’s name.

Here is an example of String concatenation using templates:

@Test
fun givenTwoStrings_concatenateWithTemplates_thenEquals() {
    val a = "Hello"
    val b = "Baeldung"
    val c = "$a $b"

    assertEquals("Hello Baeldung", c)
}

The Kotlin compiler translates this code to:

new StringBuilder().append(a).append(" ").append(b).toString()

Finally, this process is String interpolation.

6. Conclusion

In this article, we’ve learned a few ways to concatenate String objects in Kotlin.

As always, all code presented in this tutorial can be found over on GitHub.


Viewing all articles
Browse latest Browse all 4536

Trending Articles