The range is a set of data from a lower value to a larger value. In Scala, we have an easy method to create a range using to keyword.
Syntax:
startchar to endchar
object myObject { def main(args: Array[String]) { val string = ('i' to 'z').toArray for(i <- 0 to string.length-1) print(string(i) + " ") } }
Output
i j k l m n o p q r s t u v w x y z
You can also choose the value to be incremented, i.e. you can skip any number of elements while creating this range.
object myObject { def main(args: Array[String]) { val string = ('A' to 'K' by 3).toArray for(i <- 0 to string.length-1) print(string(i) + " ") } }
A D G J
This range of characters is converted to the array here, we can convert the same to List, vectors, etc using toList and toVector methods respectively.
You can also create a range of ASCII of the value of character within the given range.
array.range('startChar' , 'endChar')
object myObject { def main(args: Array[String]) { val ASCIIrange = Array.range('A', 'K') for(i <- 0 to ASCIIrange.length-1) print(ASCIIrange(i) + " ") } }
65 66 67 68 69 70 71 72 73 74
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Program to create a range of characters
Output
You can also choose the value to be incremented, i.e. you can skip any number of elements while creating this range.
Program to create a range with interval
Output
This range of characters is converted to the array here, we can convert the same to List, vectors, etc using toList and toVector methods respectively.
Create ASCII Range
You can also create a range of ASCII of the value of character within the given range.
Syntax:
Program to create a range of ASCII values
Output
need an explanation for this answer? contact us directly to get an explanation for this answer