Groovy - 如何比较字符串? [英] Groovy - How to compare the string?

查看:149
本文介绍了Groovy - 如何比较字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

以下方法不起作用。

  String str =saveMe

compareString(str)

def compareString(String str){
def str2 =saveMe
if(str2 == $ {str}){
printlnsame
} else {
println不同
}
}

也试过

  String str =印度

compareString(str)
$ b $ def compareString( String str){
def str2 =india
if(str2 == str){
printlnsame
} else {
println不同


$ / code>


解决方案

行:

  if(str2 == $ {str}){

应该是:

  if(str2 == str) {

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


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

解决方案

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

这篇关于Groovy - 如何比较字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆