Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play 2: How to compare strings in scala template?

I have a form object and I need to check if the value of a field is equal a certain string

I'm trying this but it is not working

 @if(sp.pageType.equals("customreCare")) {
   //render this specific div 
  } else {
   //render this other div
  }

but unfortunately it is not working, what is the syntax for that?

like image 478
nightograph Avatar asked Oct 02 '12 20:10

nightograph


People also ask

Can I use == to compare two strings?

You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not. On the other hand, equals() method compares whether the value of the strings is equal, and not the object itself.

How do you compare 2 strings?

Using String. equals() :In Java, string equals() method compares the two given strings based on the data/content of the string. If all the contents of both the strings are same then it returns true. If any character does not match, then it returns false.

What are the 3 ways to compare two string objects?

There are three ways to compare String in Java: By Using equals() Method. By Using == Operator. By compareTo() Method.

How do you compare two strings with StringUtils?

StringUtils class also has equalsIgnoreCase() and compareIgnoreCase() method, which are similar to the equals() and compare() method except they ignore the case of both strings. That's all about comparing two strings in Java.


1 Answers

Use == operator for comparing strings:

@defining("something") {whatToTest =>
    @if(whatToTest == "something"){ There is something } else { There is.... nothing }
}

so in your case (of course make sure that there are no typos in the conditions like customreCare ...):

@if(sp.pageType == "customreCare") {
     //render this specific div 
} else {
     //render this other div
}
like image 79
biesior Avatar answered Sep 22 '22 01:09

biesior