Trusted answers to developer questions

What is endsWith in Scala?

Get Started With Machine Learning

Learn the fundamentals of Machine Learning with this free course. Future-proof your career by adding ML skills to your toolkit — or prepare to land a job in AI or Data Science.

The endsWith function in Scala checks whether a particular string ends with the element stated as a parameter. It takes a single parameter: a string element. It then returns true if the given string ends with the element stated and false otherwise.

The illustration below shows how the endsWith function works in Scala:

How does endsWith work in Scala

Parameter

The endsWith function takes in a single parameter: a string element.

Return value

The endsWith function returns true if the given string ends with the element stated in the parameter. Otherwise, it returns false.

Example

The code snippet below shows how the endsWith function works in Scala:

object Compare {
def main(args: Array[String]) {
val myString = "Educative";
val result = myString.endsWith("e");
val result2 = myString.endsWith("v");
println("With e: " + result);
println("With v: " + result2);
}
}

As the output shows, the function:

  • returns true with “e” as a parameter, since the string “Educative” ends with an “e”.
  • returns false with “v” as a parameter, since the string “Educative” does not end with a “v”.

RELATED TAGS

scala

CONTRIBUTOR

Hassaan Waqar
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?