×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

String Concatenation in Scala

By IncludeHelp Last updated : October 20, 2024

String is an immutable collection that stores sequences of characters. There are various operations and methods defined on strings in Scala for proper functioning of the collections. One of them is concatenation.

String Concatenation

String Concatenation is joining two different strings to form a single one.

Scala provides two methods to concatenate strings. They are:

  1. concat() method
  2. '+' operator

The concat() method for string concatenation

The concat() method is used to add two strings and return a new method containing the character form both strings.

Syntax

string1.concat(string2)

Parameters

The method accepts a single parameter which is the string to be added.

Return type

It returns the concatenated string.

Program to illustrate the working of concat() method

object myObject {
    def main(args: Array[String]) {
        val string1 = "Scala "
        val string2 = "Programming Language"
        
        val concatString = string1.concat(string2)
        
        println("The concatenated string is " + concatString)
    }
}

Output:

The concatenated string is Scala Programming Language

The '+' operator for string concatenation

The '+' operator adds the two strings and returns the result of addition.

Syntax

string1 + string2

Program to illustrate the working of '+' operator

object myObject {
    def main(args: Array[String]) {
        val string1 = "Scala "
        val string2 = "Programming Language"
        
        val concatString = string1 + string2
        
        println("The concatenated string is " + concatString)
    }
}

Output:

The concatenated string is Scala Programming Language

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.