Scala programming language has a lot of methods for creating a range for integers. But you might be surprised to see that some of these methods are also employed to create the range of characters. First let's discuss the range of characters.
Range of Characters
The range of character is a series of characters with a uniform interval in between an upper and a lower limit.
Example:
Range (A to G) = A, B, C, D, E, F, G
Methods to create a range of characters
To create a range of characters some of the methods that we have used to create the integer array can be used.
The keywords to and until can be used to create a range of characters. This method will return the NumbericRange of type char. It can be referenced form:
scala.collection.immutable.NumericRange.Inclusive[Char]
'to' keyword to create a range of characters
The to keyword is used to create a range of characters from char1 to char2.
Additionally, we can add intervals to the range using the by keyword.
Syntax:
Using to keyword:
'char1' to 'char2'
Using to and by keyword:
'char1' to 'char2' by interval(int)
Example:
'a' to 'g'
-> a, b, c, d, e, f, g
'a' to 'g' by 2
-> a, d, g
Program to create range of characters using to keyword
Output:
'until' keyword to create a range of characters
Another method to create a range of characters is using the to keyword. It is used to create a range of characters from char1 to char2 (exclusive).
Additionally, we can add intervals to the range using the by keyword.
Syntax:
Example:
Program to create a range of characters using the until keyword
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer