Groovy - How to compare the string?

user1602802 picture user1602802 · Aug 16, 2012 · Viewed 229.9k times · Source

how to compare the string which is passed as a parameter

the following method is not working.

 String str = "saveMe"

 compareString(str)

 def compareString(String str){
    def str2 = "saveMe"
    if(str2==${str}){
      println "same"
    }else{
      println "not same"
    }
 }    

also tried

 String str = "India"

 compareString(str)

 def compareString(String str){
   def str2 = "india"
   if( str2 == str ) {
     println "same"
   }else{
     println "not same"
   }
 }    

Answer

ojblass picture ojblass · May 8, 2014

This should be an answer

str2.equals( str )

If you want to ignore case

str2.equalsIgnoreCase( str )