w3resource

Scala Programming: Remove the character in a given position of a given string

Scala Programming Basic Exercise-7 with Solution

Write a Scala program to remove the character in a given position of a given string. The given position will be in the range 0...string length -1 inclusive.

Sample Solution:

Scala Code:

object scala_basic {
  def test(str: String, n: Int): String = 
    {
     str.take(n) + str.drop(n + 1)
    }    
   def main(args: Array[String]): Unit = {
      println("Result: " + test("Scala", 1));
      println("Result: " + test("Scala", 0));
      println("Result: " + test("Scala", 4));
    }
}

Sample Output:

Result: Sala
Result: cala
Result: Scal

Scala Code Editor :

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

Previous: Write a Scala program to create a new string where 'if' is added to the front of a given string. If the string already begins with 'if', return the string unchanged.
Next: Write a Scala program to exchange the first and last characters in a given string and return the new string.

What is the difficulty level of this exercise?