How to check if an instance is of type String or GString in Groovy

后端 未结 3 1412
小蘑菇
小蘑菇 2021-01-03 17:56

I would like to learn what the robust way of checking, if a variable is of type string or gstring, is. I suppose it is different than in pure Java:

def var =         


        
3条回答
  •  佛祖请我去吃肉
    2021-01-03 18:54

    Another way is to use the in keyword:

    groovy:000> t = "hello"
    ===> hello
    groovy:000> t in String
    ===> true
    groovy:000> "${t}" in GString
    ===> true
    

    The in keyword is the membership operator, and gets translated to an isCase call on the class:

    groovy:000> String.isCase(t)
    ===> true
    groovy:000> GString.isCase("${t}")
    ===> true
    

提交回复
热议问题