Q:

Scala program to check the string ends with specified substring

belongs to collection: Scala String Programs

0

Here, we will create a string and then we will check the string ends with specified substring using the endsWith() method. The endsWith() method returns Boolean value. It returns true if the string ends with a specified substring. Otherwise, it will return false.

All Answers

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

Program/Source Code:

The source code to check the string ends with the specified substring is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.

// Scala program to check the string ends
// with specified substring

object Sample {
  def main(args: Array[String]) {
    var str1 = "Hello World";
    var str2 = "Hello World";

    if (str1.endsWith("World"))
      println("String str1 ends with 'World'");
    else
      println("String str1 does not ends with 'World'");

    if (str2.endsWith("Hello"))
      println("String str2 ends with 'Hello'");
    else
      println("String str2 does not ends with 'Hello'");
  }
}

Output:

String str1 ends with 'World'
String str2 does not ends with 'Hello'

In the above program, we used an object-oriented approach to create the program. And, we created an object Sample. Here, we defined main() function. The main() function is the entry point for the program.

In the main() function, we created two string variables str1str2. Both strings are initialized with "Hello World". Then we check strings ends with specified substring using endswith() method and print the appropriate message on the console screen.

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

total answers (1)

Scala String Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Scala program to get bytes array from string using... >>
<< Scala program to check the string starts with a sp...