markusw
markusw

Reputation: 2055

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. So why is this and how can I collect all characters of a String?

Upvotes: 1

Views: 408

Answers (2)

Jeff Scott Brown
Jeff Scott Brown

Reputation: 27200

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...
}

Upvotes: 4

dmahapatro
dmahapatro

Reputation: 50245

In Groovy, a single character is also a String. In order to get each String as a Character use as:

"testString".collect { it as char }

Upvotes: 2

Related Questions