Groovy - How to compare the string?

后端 未结 7 1647
醉话见心
醉话见心 2021-02-04 23:03

how to compare the string which is passed as a parameter

the following method is not working.

 String str = \"saveMe\"

 compareString(str)

 def         


        
相关标签:
7条回答
  • 2021-02-04 23:28

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

    def compareString = {
       it == "india" ? "same" : "not same"
    }    
    
    compareString("India")
    
    0 讨论(0)
  • 2021-02-04 23:30

    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"){

    }

    0 讨论(0)
  • 2021-02-04 23:34

    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.

    0 讨论(0)
  • 2021-02-04 23:35
    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
    
    0 讨论(0)
  • 2021-02-04 23:40

    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.

    0 讨论(0)
  • 2021-02-04 23:41

    This should be an answer

    str2.equals( str )

    If you want to ignore case

    str2.equalsIgnoreCase( str )

    0 讨论(0)
提交回复
热议问题