Search code examples
groovy

Groovy - How to compare the string?


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"
   }
 }    

Solution

  • This line:

    if(str2==${str}){
    

    Should be:

    if( str2 == str ) {
    

    The ${ and } will give you a parse error, as they should only be used inside Groovy Strings for templating