Write a Scala program to read a string and return true if it ends with a specified string of length 2.
object Scala_String { def test(str1: String, end_str: String): Boolean = { val len = str1.length val ng = end_str if (len < 2) false else if (ng.equals(str1.substring(len - 2, len))) true; else false } def main(args: Array[String]): Unit = { var str1 = "String"; var end_str = "ng" println("The given strings is: " + str1) println("The string containing " + end_str + " at last: " + test(str1, end_str)) end_str = "gn" println("The given strings is: " + str1) println("The string containing " + end_str + " at last: " + test(str1, end_str)) } }
Sample Output:
The given strings is: String The string containing ng at last: true The given strings is: String The string containing gn at last: false
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Sample Output:
need an explanation for this answer? contact us directly to get an explanation for this answer