本文介绍了如何在Groovy中检查实例的类型为String还是GString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果变量是string或gstring类型,我想学习什么健壮的检查方法.我想它与纯Java中的有所不同:
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 = someFunc();
if (var instanceof String || var instanceof GString) {
String str = var;
}
我问,是因为我不想错过一个Groovy特有的功能,该功能会导致难以调试的错误.
I ask, because I do not want to be surprised that I have missed a Groovy specific feature that causes a hard to debug bug.
推荐答案
测试实例应该可以工作:
Instanceof tests should work:
assert "fasd" instanceof String
assert "${this}" instanceof GString
这篇关于如何在Groovy中检查实例的类型为String还是GString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!