Kotlin IntRange, LongRange & CharRange

Like to share

1. Overview

In this article, we will learn the Kotlin IntRange, LongRange & CharRange along with few examples.

2. Kotlin IntRange class

So why would you need IntRange? If you want to check any Int value is in the expected range, then use IntRange. The syntax of IntRange is

<init>(start: Int, end: Int)

start - minimum value in the range (inclusive)

end - maximum value in the range (inclusive)

As you know, alpha represents the transparency of color and ranges from 0 to 255. If you had to get the alpha value from the user and validate the input, then use IntRange class and check if the user-provided value is in the expected range.

2.1. IntRange example

validateUserInput(245);
fun validateUserInput(value: Int) : Boolean {
    val intRange = IntRange(0, 255);
    return intRange.contains(value);
}

Result:

true

2.2. IntRange equals function

If you like to compare two IntRange instances and check if both are supporting the same range of values.

fun main() {
    System.out.println(compareIntRanges());
}
fun compareIntRanges() : Boolean {
    val intRange = IntRange(0, 255);
    val otherRange = IntRange(0, 155);
    return intRange.equals(otherRange);
}

Result:

false

fun main() {
    System.out.println(compareIntRanges());
}
fun compareIntRanges() : Boolean {
    val intRange = IntRange(0, 255);
    val otherRange = IntRange(0, 255);
    return intRange.equals(otherRange);
}

Result:

true

2.3. Kotlin IntRange to List

2.3.1. IntRange toList

Returns a List containing all elements of the given range

fun main() {
    System.out.println(listOfIntegers());
}
fun listOfIntegers() : List<Int> {
    val intRange = IntRange(-10, -1);
    return intRange.toList();
}

Result:

[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1]

2.3.2 IntRange take

Returns a list containing first n elements.

fun main() {
    System.out.println(firstNElements());
}
fun firstNElements() : List<Int> {
    val intRange = IntRange(0, 255);
    return intRange.take(10);
}

Result:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

2.3.3. IntRange drop

Returns a list containing all elements except first n elements.

fun main() {
    System.out.println(dropNElements());
}
fun dropNElements() : List<Int> {
    val intRange = IntRange(0, 255);
    return intRange.drop(250);
}

Result:

[250, 251, 252, 253, 254, 255]

2.3.4. IntRange filter

Returns a list containing only elements matching the given predicate.

fun main() {
    System.out.println(filter());
}
fun filter() : List<Int> {
    val intRange = IntRange(0, 255);
    return intRange.filter({ value ->
        value % 10 == 0
    });
}

Result:

[0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 250]

2.3.5. Kotlin IntRange annotation

Kotlin doesn’t support IntRange as an annotation. If you try to use it, then you will see a warning as

Kotlin IntRange annotation
Kotlin IntRange annotation warning

3. Kotlin LongRange

This is the same as IntRange. But you can use a maximum value up to 9,223,372,036,854,775,807 (2 63- 1). The syntax of LongRange is

<init>(start: Int, end: Int)

start - minimum value in the range (inclusive)

end - maximum value in the range (inclusive)

4. Kotlin CharRange

The type Char represents characters. Character literals go in single quotes: '1'. Here, you can use CharRange to check if the provided char is in the expected range. The syntax of CharRange is

<init>(start: Int, end: Int)

start - minimum value in the range (inclusive)

end - maximum value in the range (inclusive)

4.1. CharRange concat

Let’s see a simple example to understand the CharRange. Here, we print characters from the ‘A’ to ‘Z’ range.

fun main() {
    System.out.println(listOfChars());
}
fun listOfChars() : List<Char> {
    val charRange = CharRange('A', 'Z');
    return charRange.toList();
}

Result:

[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]

Let’s see another example in which we concatenate two CharRange (CharRange concatenation)

fun main() {
    val first = CharRange('A', 'Z');
    val last = CharRange('0', '9');
    val end = first + last;
    print(end)
}

Result:

[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, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

5. Conclusion

In this article, we have seen few examples for Kotlin IntRange. For a complete list of functions available with IntRange, refer to the Kotlin official documentation. If you would like to learn more on Kotlin, we recommend you to refer to our Kotlin articles.

Leave a Reply

Your email address will not be published. Required fields are marked *