Q:

Kotlin program of character range using rangeTo() function

belongs to collection: Kotlin Ranges Programs

0

rangeTo() Function

The rangeTo() function is similar to (..) operator. It creates a range up to the value passed as an argument.

Here, we will demonstrate the example of creating a character range using the rangeTo() function.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Example 1:

fun main(args : Array<String>){
	// creating character ranges
	println("Character range 1:")	
	for(x in 'a'.rangeTo('g')){
		println(x)
	}
	println()

	println("Character range 2:")	
	for(x in 'g'.rangeTo('p')){
		println(x)
	}
	println()
}

Output:

Character range 1:
a
b
c
d
e
f
g

Character range 2:
g
h
i
j
k
l
m
n
o
p

Example 2:

fun main(args : Array<String>){
	// creating character ranges
	println("Character range 1:")	
	for(x in 'a'.rangeTo('g') step 3){
		println(x)
	}
	println()

	println("Character range 2:")	
	for(x in 'g'.rangeTo('p') step 3){
		println(x)
	}
	println()
}

Output:

Character range 1:
a
d
g

Character range 2:
g
j
m
p

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Kotlin program of integer range using downTo() fun... >>
<< Kotlin program of integer range using rangeTo() fu...