简体   繁体   English

Groovy收集字符串

[英]Groovy collect over a String

When I collect over a String I was expecting that it would be of type char , but it is java.lang.String . 当我收集一个String时,我期望itchar类型的,但是它是java.lang.String So why is this and how can I collect all characters of a String? 那么为什么会这样,又如何收集字符串的所有字符呢?

You can do something like this... 你可以做这样的事情...

someString.collect { 
    def c = it as char
    // carry on...
}

Or... 要么...

someString.chars.collect {
    // it will be a char
    // carry on...
}

In Groovy, a single character is also a String. 在Groovy中,单个字符也是字符串。 In order to get each String as a Character use as: 为了获得每个字符串作为字符,请使用:

"testString".collect { it as char }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM