Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
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:

object scala_basic {
  def test(str: String): String = { 
      if (str.drop(1).startsWith("yt")) str.replaceFirst("yt", "") 
      else str
    }
     
   def main(args: Array[String]): Unit = {
      println("Result: " + test("Scala"));
      println("Result: " + test("yytade"));
      println("Result: " + test("ytsues"));      
    }
  }

Sample Output:

Result: Scala
Result: yade
Result: ytsues

Scala Code Editor :

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

Previous: Write a Scala program to 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: Write a Scala program to check the largest number among three given integers.

What is the difficulty level of this exercise?