109

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"
   }
 }    
amarillion
  • 24,487
  • 15
  • 68
  • 80
user1602802
  • 1,139
  • 2
  • 8
  • 7

7 Answers7

149

This should be an answer

str2.equals( str )

If you want to ignore case

str2.equalsIgnoreCase( str )

ojblass
  • 21,146
  • 22
  • 83
  • 132
  • 41
    Downvoting because the original question did not specify that it should ignore care. Also, groovy supports the `==` operator for string comparison (in contrast with Java, where `==` does identity comparison, not string comparison). – brianmearns Sep 24 '19 at 19:15
  • 1
    I adjusted the answer to be case sensitive – ojblass Dec 17 '19 at 16:26
  • 1
    Is there a short alias for equalsIgnoreCase in Groovy? – Simon Logic Feb 05 '20 at 15:51
  • 1
    I think you could create a wrapper function called eIC or something similar. Depends on how much it actually bugs you. – ojblass Feb 24 '20 at 03:25
  • 11
    Warning. `.equals()` does *not* behave the same in Groovy as it does in Java. Example [here](http://grails.asia/groovy-compare-string) -- scroll down to 'GString and String'. Basically both items have to be of the same type of String class. Since the String class is implicit in the assignment -- GString is a Groovy language construct and String is an inline definition, something like `def foo = "foo"` ... `"${foo}".equals("foo")` will return `false`. – ingyhere Apr 23 '20 at 21:41
114

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

tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • @user1602802 It does in the groovy console. What errors are you getting? – tim_yates Aug 16 '12 at 09:37
  • 26
    @user1602802 Are you comparing `India` with a capital `I` and `india` with a lower-case `i` like in the edit to your question? `India != india`. They are different. – tim_yates Aug 16 '12 at 09:39
  • 7
    Coming from Java this might seem wrong but `==` operator does string comparison like `equals` in Groovy. – Federico Nafria Apr 09 '21 at 07:52
6

If you don't want to check on upper or lowercases you can use the following method.

String str = "India" 
compareString(str) 

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

So now if you change str to "iNdIa" it'll still work, so you lower the chance that you make a typo.

Dieterg
  • 16,118
  • 3
  • 30
  • 49
0

The shortest way (will print "not same" because String comparison is case sensitive):

def compareString = {
   it == "india" ? "same" : "not same"
}    

compareString("India")
Simek
  • 347
  • 2
  • 15
0
String str = "saveMe"
compareString(str)

def compareString(String str){
  def str2 = "saveMe"

  // using single quotes
  println 'single quote string class' + 'String.class'.class
  println str + ' == ' + str2 + " ? " + (str == str2)
  println ' str = ' +  '$str' //  interpolation not supported

  // using double quotes, Interpolation supported
  println "double quoted string with interpolation " + "GString.class $str".class
  println "double quoted string without interpolation " + "String.class".class
  println "$str equals $str2 ? " + str.equals(str2) 
  println '$str == $str2 ? ' + "$str==$str2"
  println '${str == str2} ? ' + "${str==str2} ? "

  println '$str equalsIgnoreCase $str2 ? ' + str.equalsIgnoreCase(str2)   

  println '''
  triple single quoted Multi-line string, Interpolation not supported $str ${str2}
  Groovy has also an operator === that can be used for objects equality
  === is equivalent to o1.is(o2)
  '''
  println '''
  triple quoted string 
  '''
  println 'triple single quoted string ' + '''' string '''.class

  println """ 
  triple double quoted Multi-line string, Interpolation is supported $str == ${str2}
  just like double quoted strings with the addition that they are multiline
  '\${str == str2} ? ' ${str == str2} 
  """
  println 'triple double quoted string ' + """ string """.class
} 

output:

single quote string classclass java.lang.String
saveMe == saveMe ? true
str = $str
double quoted string with interpolation class org.codehaus.groovy.runtime.GStringImpl
double quoted string without interpolation class java.lang.String
saveMe equals saveMe ? true
$str == $str2 ? saveMe==saveMe
${str == str2} ? true ? 
$str equalsIgnoreCase $str2 ? true 

triple single quoted Multi-line string, Interpolation not supported $str ${str2}
Groovy has also an operator === that can be used for objects equality
=== is equivalent to o1.is(o2)


triple quoted string 

triple single quoted string class java.lang.String

triple double quoted Multi-line string, Interpolation is supported saveMe == saveMe
just like double quoted strings with the addition that they are multiline
'${str == str2} ? ' true 

triple double quoted string class java.lang.String
velocity
  • 1,630
  • 21
  • 24
-3

In Groovy, null == null gets a true. At runtime, you won't know what happened. In Java, == is comparing two references.

This is a cause of big confusion in basic programming, Whether it is safe to use equals. At runtime, a null.equals will give an exception. You've got a chance to know what went wrong.

Especially, you get two values from keys not exist in map(s), == makes them equal.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
-7

use def variable, when you want to compare any String. Use below code for that type of comparison.

def variable name = null

SQL query give you some return. Use function with return type def.

def functionname(def variablename){

return variable name

}

if ("$variable name" == "true"){

}