I wonder how I can convert the following ClosedRange<String> to List<String>:
val letters: ClosedRange<String> = ("a".."z")
I wonder how I can convert the following ClosedRange<String> to List<String>:
val letters: ClosedRange<String> = ("a".."z")
Copyright © 2021 Jogjafile Inc.
ClosedRangeknows nothing about items in the middle. It is basically a start and an end item.The problem here is that you used strings and you should use chars. There is no meaningful answer on what strings exist between the string
"a"and"z". What about:"hello"? Is it between them or not? But if we discuss chars, then obviously, we know what chars exist between'a'and'z', we can iterate over them or create a list:Please be aware this code returns
List<Char>, notList<String>. If you need strings, we can easily convert from chars:We don't need
toList()anymore, becausemapimplicitly converted from a range to a list.