Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

scala

What is concat in Scala?

Samia Ishaque

The concat method in Scala is used to concatenate two strings to create a third string. Strings in Scala are immutable and cannot be changed once defined, so concatenation helps us overcome this by letting us join two strings together. Below is a general syntax for how this is done:

string1.concat(string2)

Parameters

The concat method only takes one compulsory input, which is the string you wish to add to the end of the original string.

Return value

The concat method returns a new concatenated string.

Example

Below is an example of how you can use the concat method to join two strings. We use the concat method to concatenate string2 to string1 and form a third string that reads Hello World!

Another way of achieving the same result is by adding the + sign between two strings:

object Main extends App {
    val string1 = "Hello "
    val string2 = "World!"
    val string3 = string1.concat(string2)
    print(string3+"\n")

    val string4 = "Hello "
    val string5 = "World!"
    val string6 = string4 + string5
    print(string6)

}

RELATED TAGS

scala

CONTRIBUTOR

Samia Ishaque
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring