w3resource

Scala Programming: Create a new String object with the contents of a character array

Scala Programming String Exercise-6 with Solution

Write a Scala program to create a new String object with the contents of a character array.

Sample Solution:

Scala Code:

object Scala_String {
  def main(args: Array[String]): Unit = {
    // Character array with data.
    val arr_num = Array('1', '2', '3', '4', '5', '6');

    // Create a String containig the contents of arr_num
    // starting at index 1 for length 2.
    val str = String.copyValueOf(arr_num, 2, 3);

    // Display the results of the new String.
    println("\nThe book contains " + str + " pages.\n");
  }
}

Sample Output:

The book contains 345 pages.

Scala Code Editor :

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

Previous: Write a Scala program to test if a given string contains the specified sequence of char values.
Next: Write a Scala program to check whether a given string ends with the contents of another string.

What is the difficulty level of this exercise?