如何在Groovy中拆分整数?

sri*_*ram 1 groovy

我想在groovy中拆分给定的整数.比如说,

 def a = 198
    println(a.split())
Run Code Online (Sandbox Code Playgroud)

但是我得到一个错误,因为我无法将该split方法应用于整数,所以有任何方法或方法来执行此操作.我想要的是这个:

   def a = 198
   // what to do here?
   // i want to get an output like 1,9,8
   // any ideas?
Run Code Online (Sandbox Code Playgroud)

tim*_*tes 6

你只能拆分字符串,虽然你可能想要的只是做:

"$a".collect { it as Integer }
Run Code Online (Sandbox Code Playgroud)

(作为String字符集合工作)

另一种选择是:

"$a"*.toInteger()
Run Code Online (Sandbox Code Playgroud)