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

  • برمجة سكالا

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.

الأجوبة

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
هل كان المحتوى مفيد؟

معلومات ذات صلة

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...