Search code examples
stringgroovyalphabet

Get english alphabet as String


I recently started learning Groovy. My task is to get english alphabet. I don't understand, why code

println 'a'..'z'

working fine, but when i try to get it class

println 'a'..'z'.class

ClassCastException occures. I want to do something like this:

return 'a'..'z'.toString()

Can u help me? What is best way to get english alphabet in String?


Solution

  • groovy:000> println(('a'..'z').class)
    class groovy.lang.ObjectRange
    

    It boils down to parsing issues.

    'a'..'z'.class
    

    is interpreted as meaning "From 'a' to 'z'.class".

    You can't write

    println ('a'..'z').class
    

    because that would be trying to call class on the return of println, which doesn't return anything.

    The quickest way to get this as a string is simply:

    ('a'..'z').join()