BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

String Concatenation in Kotlin

Last Updated: December 23, 2017 by Chaitanya Singh | Filed Under: Kotlin Tutorial

String concatenation is joining two or more strings together. In this guide, we will see three ways to concatenate strings in Kotlin.

1. String templates

One of the simplest way of concatenating strings is by using String templates.

fun main(args: Array<String>) {

    val str1 = "hello"
    val str2 = "hi"
    val str3 = "bye"

    // string interpolation
    val str4 = "$str1 $str2 $str3"

    // displaying concatenated string
    println(str4)
}

Output:

hello hi bye

Note: We have space in between the strings because we have given the space while using string template in str4.

2. String concatenation using Plus (+) Arithmetic Operator

We have seen in the Kotlin Operators tutorial that + arithmetic operator is used for addition. The same operator can also be used for string concatenation as shown in the example below. The + operator is translated into a plus() function.

fun main(args: Array<String>) {

    val str1 = "hello"
    val str2 = "hi"
    val str3 = "bye"

    // joining using + operator
    // can also be written like this: 
    // val str4 = str1.plus(str2).plus(str3)
    val str4 = str1 + str2 + str3

    // displaying concatenated string
    println(str4)
}

Output:

hellohibye

3. Concatenation using StringBuilder

We can join strings using StringBuilder and then assign it to string by using a toString() function on the StringBuilder object.

fun main(args: Array<String>) {

    val str1 = "hello"
    val str2 = "hi"
    val str3 = "bye"

    // Obtaining StringBhuilder Object
    val sb = StringBuilder()

    //joining strings
    sb.append(str1).append(str2).append(str3)

    // StringBuilder to String
    val str4 = sb.toString()

    //displaying final string
    println(str4)
}

Top Related Articles:

  1. Kotlin Nested and Inner Class with examples
  2. Kotlin Keywords, Soft Keywords and Identifiers
  3. Kotlin Class and Objects – Object Oriented Programming (OOP)
  4. Kotlin Abstract Class with examples
  5. Kotlin Constructors with examples

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Kotlin Tutorial

  • Kotlin Tutorial
  • Kotlin in Eclipse IDE
  • Kotlin in IntelliJ
  • First Kotlin Program
  • Kotlin Keywords
  • Kotlin Variables
  • Kotlin Type Casting
  • Kotlin operators
  • Kotlin Input/Output
  • Kotlin Comments

Kotlin String

  • Kotlin String

Kotlin Array

  • Kotlin Array
  • Kotlin Range

Control Flow

  • Kotlin if expression
  • Kotlin when expression
  • Kotlin for loop
  • Kotlin while loop
  • Kotlin do-while loop
  • Kotlin continue
  • Kotlin break

Kotlin Function

  • Kotlin Function
  • Kotlin Recursion
  • Kotlin default and named arguments
  • Lambda functions
  • Higher Order Function

Exception Handling

  • Exception Handling
  • Kotlin try catch
  • Multiple catch blocks
  • Nested try catch
  • Throw keyword
  • Kotlin try expression

Kotlin OOP

  • Class and Objects
  • Kotlin Constructors
  • Kotlin Inheritance
  • Visibility Modifiers
  • Kotlin abstract class
  • Kotlin Interfaces
  • Nested and Inner Class
  • Kotlin Data Class
  • Kotlin Sealed Class

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap