Converting a string to int in Groovy

前端 未结 13 2244
陌清茗
陌清茗 2020-11-30 18:45

I have a String that represents an integer value and would like to convert it to an int. Is there a groovy equivalent of Java\'s Integer.pars

相关标签:
13条回答
  • 2020-11-30 19:30

    The way to use should still be the toInteger(), because it is not really deprecated.

    int value = '99'.toInteger()
    

    The String version is deprecated, but the CharSequence is an Interface that a String implements. So, using a String is ok, because your code will still works even when the method will only work with CharSequence. Same goes for isInteger()

    See this question for reference : How to convert a String to CharSequence?

    I commented, because the notion of deprecated on this method got me confuse and I want to avoid that for other people.

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