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:

// Define an object named scala_basic
object scala_basic {
  // Define a function named test with parameters str of type String and n of type Int, returning a String
  def test(str: String, n: Int): String = 
    {
      // Take the substring of str from the beginning up to index n (exclusive), and concatenate it with
      // the substring of str starting from index n+1 to the end
      str.take(n) + str.drop(n + 1)
    }    

   // Define the main method, which is the entry point of the program
   def main(args: Array[String]): Unit = {
      // Print the result of calling test with the arguments "Scala" and 1
      println("Result: " + test("Scala", 1))

      // Print the result of calling test with the arguments "Scala" and 0
      println("Result: " + test("Scala", 0))

      // Print the result of calling test with the arguments "Scala" and 4
      println("Result: " + test("Scala", 4))
    }
}

Sample Output:

Result: Sala
Result: cala
Result: Scal

Explanation:

Here is the break down of the said Scala code:

  • object scala_basic {: This declares an object named scala_basic.
  • def test(str: String, n: Int): String =: This line defines a function named test that takes two parameters - a string str and an integer n. The function returns a string. It manipulates the input string by taking a substring up to index n (exclusive) and concatenating it with the substring starting from index n+1 to the end.
  • str.take(n) + str.drop(n + 1): This is the implementation of the test function. It uses the take method to get the substring from the beginning of str up to index n (exclusive), and then uses the drop method to get the substring starting from index n+1 to the end. These substrings are then concatenated.
  • def main(args: Array[String]): Unit = {: This line defines the main method, which is the entry point of the program. It takes an array of strings (args) as its parameter and returns Unit (similar to void in other languages).
  • println("Result: " + test("Scala", 1)): This line calls the "test()" function with the arguments "Scala" and 1, concatenates the result with the string "Result: ", and prints the entire string to the console.
  • println("Result: " + test("Scala", 0)): Similar to the previous line, this calls the "test ()" function with the arguments "Scala" and 0.
  • println("Result: " + test("Scala", 4)): Another call to the test function with the arguments "Scala" and 4.

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

Previous: 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: Exchange the first and last characters in a given string and return the new string.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/scala-exercises/basic/scala-basic-exercise-7.php