Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare two strings in scala?

I want to compare two strings in scala. for example,

My Strings are:

scala java scala java c++ scala c++ 

I want to compare the string

" scala c++" with each strings

Results should be,

scala c++ = scala java   // false scala c++ = scala java c++  // false scala c++ = scala c++   // true 
like image 603
Rosy b Avatar asked Oct 10 '14 10:10

Rosy b


People also ask

Can you compare strings in Scala?

In Scala, the == method defined in the AnyRef class first checks for null values, and then calls the equals method on the first object (i.e., this) to see if the two objects are equal. As a result, you also don't have to check for null values when comparing strings. In idiomatic Scala you never use null values.

What is == in Scala?

if x equals y is true if both x and y have the same value. They do not need to refer to the identical instance. Hence, the equals method in Java and equals method in Scala behaves same. The == and !=

What is the difference between .equals and == in Scala?

== is a final method, and calls . equals , which is not final. This is radically different than Java, where == is an operator rather than a method and strictly compares reference equality for objects.


1 Answers

In Scala you can use == for equality

scala> "scala c++" == "scala java" res0: Boolean = false scala> "scala c++" == "scala java c++" res1: Boolean = false scala> "scala c++" == "scala c++" res2: Boolean = true 

The == method is defined in the AnyRef class. Since the methods first checks for null values, and then calls the equals method on the first object to see if the two objects are equals you dont have to do a special null check;

"test" == null res0: Boolean = false 

See the Scala getting started guide and strings

From "An Overview of the Scala Programming Language Second Edition";

"The equality operation == between values is designed to be transparent with respect to the type's representation. For value types, it is the natural (numeric or boolean) equality. For reference types, == is treated as an alias of the equals method from java.lang.Object. That method is originally defined as reference equality, but is meant to be overridden in subclasses to implement the natural notion of equality for these subclasses. For instance, the boxed versions of value types would implement an equals method which compares the boxed values. By contrast, in Java, == always means reference equality on reference types. While this is a bit more efficient to implement, it also introduces a serious coherence problem because boxed versions of equal values might no longer be equal with respect to ==. Some situations require reference equality instead of user-dened equality. An example is hash-consing, where eciency is paramount. For these cases, class AnyRef defines an additional eq method, which cannot be overridden, and is implemented as reference equality (i.e., it behaves like == in Java for reference types)."

like image 78
oluies Avatar answered Sep 19 '22 12:09

oluies