w3resource

Scala Programming: Concatenate a given string to the end of another string

Scala Programming String Exercise-4 with Solution

Write a Scala program to concatenate a given string to the end of another string.

Sample Solution:

Scala Code:

object Scala_String {

  def main(args: Array[String]): Unit = {
    val str1 = "Scala Exercises and ";
    val str2 = "Python Exercises";
    println("Original strings:")
    println("String 1: " + str1);
    println("String 2: " + str2);

    // Concatenate the two strings together.
    val str3 = str1.concat(str2);
    // Display the new String.
    println("The concatenated string: " + str3);
  }
}

Sample Output:

Original strings:
String 1: Scala Exercises and 
String 2: Python Exercises
The concatenated string: Scala Exercises and Python Exercises

Scala Code Editor :

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Scala program to compare two strings lexicographically.
Next: Write a Scala program to test if a given string contains the specified sequence of char values.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.