98

如何比较作为参数传递的字符串

以下方法不起作用。

 String str = "saveMe"

 compareString(str)

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

也试过

 String str = "India"

 compareString(str)

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

7 回答 7

133

这应该是一个答案

str2.equals(str)

如果你想忽略大小写

str2.equalsIgnoreCase(str)

于 2014-05-08T18:57:03.660 回答
107

这一行:

if(str2==${str}){

应该:

if( str2 == str ) {

和会给你一个解析错误,因为它们应该只在 Groovy 字符串中用于${模板}

于 2012-08-16T09:33:53.533 回答
6

如果您不想检查大小写,可以使用以下方法。

String str = "India" 
compareString(str) 

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

因此,现在如果您将 str 更改为“iNdIa”,它仍然可以工作,因此您可以降低打错字的机会。

于 2012-08-16T09:45:35.530 回答
0

最短的方法(将打印“不一样”,因为字符串比较区分大小写):

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

compareString("India")
于 2013-01-20T12:15:54.387 回答
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
} 

输出:

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
于 2020-12-05T02:04:54.953 回答
-3

在 Groovy 中,null == null得到一个true. 在运行时,您不会知道发生了什么。在 Java 中,==是比较两个引用。

这是基本编程中大混乱的原因,使用equals是否安全。在运行时, null.equals 将给出异常。你有机会知道出了什么问题。

特别是,您从地图中不存在的键中获取两个值,==使它们相等。

于 2014-12-01T21:09:59.193 回答
-7

当你想比较任何字符串时,使用 def 变量。使用下面的代码进行这种类型的比较。

def 变量名 = null

SQL查询给你一些回报。使用返回类型为 def 的函数。

def 函数名(def 变量名){

返回变量名

}

if ("$变量名" == "true"){

}

于 2016-03-03T12:50:01.930 回答