How to create a range of characters (Lists, Sequence) in Scala?

Here, we will learn a method to create a range of characters in Scala, with examples.
Submitted by Shivang Yadav, on October 09, 2020 [Last updated : March 11, 2023]

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

object MyClass {
    def main(args: Array[String]) {
        val R1 = 'a' to 'z'; 
        
        println("Printing the range created using the 'to' keyword ")
        R1.foreach{ch:Char => print(ch + " ")}
        
        val R2 = 'a' to 'z' by 3; 
        
        println("\nPrinting the range created using the 'to' and 'by' keyword ")
        R2.foreach{ch:Char => print(ch + " ")}
    }
}

Output

Printing the range created using the 'to' keyword 
a b c d e f g h i j k l m n o p q r s t u v w x y z 
Printing the range created using the 'to' and 'by' keyword 
a d g j m p s v y 

'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

Using to keyword:
	'char1' until 'char2' 

Using to and by keyword:
	'char1' until 'char2' by interval(int)

Example

'a' until 'l'
-> a, b, c, d, e, f, g, h, i, j, k

'a' to 'l' by 3 
-> a, e, i

Program to create a range of characters using the until keyword

object MyClass {
    def main(args: Array[String]) {
        val R1 = 'a' until 'z'; 
        
        println("Printing the range created using the 'until' keyword ")
        R1.foreach{ch:Char => print(ch + " ")}
        
        val R2 = 'a' until 'z' by 2; 
        
        println("\nPrinting the range created using the 'until' and 'by' keyword ")
        R2.foreach{ch:Char => print(ch + " ")}
    }
}

Output

Printing the range created using the 'to' keyword 
a b c d e f g h i j k l m n o p q r s t u v w x y z 
Printing the range created using the 'to' and 'by' keyword 
a d g j m p s v y 

Scala List Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.