Q:

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

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now