w3resource

Scala Programming: Check whether a string 'yt' appears at index 1 in a given string

Scala Programming Basic Exercise-18 with Solution

Write a Scala program to check whether a string 'yt' appears at index 1 in a given string. If it appears return a string without 'yt' otherwise return the original string.

Sample Solution:

Scala Code:

// Define an object named scala_basic
object scala_basic {
  // Define a function named test with parameter str of type String, returning a String
  def test(str: String): String = { 
      // Check if the string, excluding the first character, starts with "yt"
      if (str.drop(1).startsWith("yt")) str.replaceFirst("yt", "") 
      // If the condition is not met, return the original string
      else str
    }
     
   // 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 argument "Scala"
      println("Result: " + test("Scala"))
      
      // Print the result of calling test with the argument "yytade"
      println("Result: " + test("yytade"))
      
      // Print the result of calling test with the argument "ytsues"
      println("Result: " + test("ytsues"))      
    }
}

Sample Output:

Result: Scala
Result: yade
Result: ytsues

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): String = {: This line defines a function named test that takes a parameter str of type String and returns a String. The function performs a conditional check on the string.
  • if (str.drop(1).startsWith("yt")) str.replaceFirst("yt", ""): This line checks if the substring of str from the second character onward starts with "yt." If true, it replaces the first occurrence of "yt" with an empty string using replaceFirst. Otherwise, it returns the original string.
  • else str: If the condition in the if statement is not true, this part of the code is executed, and it returns the original string.
  • }: Closes the test function.
  • 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")): This line calls the "test()" function with the argument "Scala" and prints the result to the console.
  • println("Result: " + test("yytade")): Another call to the "test()" function with the argument "yytade."
  • println("Result: " + test("ytsues")): Another call to the "test()" function with the argument "ytsues."

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

Previous: Check whether two given integer values are in the range 20..50 inclusive. Return true if 1 or other is in the said range otherwise false.
Next: Check the largest number among three given integers.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.