7 minutes read

Introduction

In programming languages, character sequences are used to represent a sequence of characters. As a modern programming language, Kotlin has its own way of representing a sequence of characters, known as CharSequence.

public interface CharSequence {
    public val length: Int

    public operator fun get(index: Int): Char

    public fun subSequence(startIndex: Int, endIndex: Int): CharSequence
}

In this topic, we will discuss what CharSequence is, how it differs from String, and how it can be used in Kotlin programming.

CharSequence (sequence of chars)

CharSequence in Kotlin is an interface that represents a sequence of characters. It is designed to be immutable, which means that its contents cannot be changed after it has been created. The CharSequence interface defines several methods that can be used to access and manipulate the characters in a sequence, including the length(), charAt(index), and subSequence(startIndex, endIndex) methods.

Here's an example of using CharSequence in Kotlin:

fun main() {
    val charSequence: CharSequence = "example"
    println("CharSequence: $charSequence")
    
    val charAt: Char = charSequence[0]
    println("Char at index 0: $charAt")
    
    val subSequence: CharSequence = charSequence.subSequence(1, 4)
    println("Sub-sequence from index 1 to 4: $subSequence")
    
    val contains: Boolean = charSequence.contains("amp", ignoreCase = true)
    println("CharSequence contains 'amp': $contains")
}

The above code creates a CharSequence object charSequence with the value "example". Then it demonstrates some of the methods available for CharSequence:

  • charAt() returns the character at the specified index (in this case, the first character).

  • subSequence() returns a new CharSequence representing a range of characters from the original CharSequence (in this case, the characters from index 1 to 4).

  • contains() checks if a specified character or character sequence is present in the CharSequence (in this case, checking if "amp", case-insensitive, is present in the CharSequence).

Output:

CharSequence: example
Char at index 0: e
Sub-sequence from index 1 to 4: xam
CharSequence contains 'amp': true

CharSequence vs String (difference)

The most significant difference between CharSequence and String is that CharSequence is an interface, whereas String is a concrete class in Kotlin. CharSequence can be thought of as a more general interface that String implements. Any String is a CharSequence, but not every CharSequence is a String. Another difference between CharSequence and String is properties and functions. Here's an example that illustrates the difference:

fun main() {
    val string: String = "Hello, World!"
    val charSequence: CharSequence = string

    println(string.uppercase()) // prints "HELLO, WORLD!"
    // The following line won't compile because CharSequence doesn't have an uppercase() function
    // println(charSequence.uppercase())
}

In the above example, we create a String and a CharSequence that refers to the same string. We can call the upperCase() function on the String object to convert it to uppercase. However, we cannot call upperCase() on the CharSequence object, as the CharSequence interface doesn't define this function.

In summary, String is a concrete class that implements the CharSequence interface and provides additional functionality, while CharSequence is an interface that defines a sequence of characters that can be implemented by different classes.

CharSequence usage (when to use, how to convert to String)

CharSequence can be used in situations where we need to represent a sequence of characters but do not know the exact implementation of the sequence beforehand. For example, if we want to read a large file into memory, we may not know the exact length of the file, so we can use CharSequence to represent the file's contents. CharSequence can be also used to implement custom string classes that need to represent a sequence of characters.

To convert CharSequence to String, we can simply use the toString() method. The toString() method returns a new string that contains the characters present in the CharSequence.

fun main() {
    val charSequence: CharSequence = "Hello, Kotlin!"
    val str: String = charSequence.toString()
    println(str) // Hello, Kotlin!
}

In the above example, we create a CharSequence object with the value "Hello, Kotlin!" Then we call the toString() method on the CharSequence object to convert it to a string. Finally, we print the resulting string to the console.

Note that any String object can be also used as a CharSequence object, since String implements the CharSequence interface.

Conclusion

In conclusion, CharSequence is a powerful interface in Kotlin that represents a sequence of characters. It is immutable and provides several methods that can be used to access and manipulate the characters in the sequence. It differs from String in that it is an interface. CharSequence can be used in situations where we need to represent a sequence of characters but do not know the exact implementation of the sequence beforehand.

67 learners liked this piece of theory. 8 didn't like it. What about you?
Report a typo