In kotlin source code, I can't understand how to implement of length of String.kt , it is as following:
package kotlin
public class String : Comparable<String>, CharSequence {
companion object {}
/**
* Returns a string obtained by concatenating this string with the string representation of the given [other] object.
*/
public operator fun plus(other: Any?): String
public override val length: Int
public override fun get(index: Int): Char
public override fun subSequence(startIndex: Int, endIndex: Int): CharSequence
public override fun compareTo(other: String): Int}
var len:Int = "abc".length; // len = 3 where to run the length??
where to implement the length function?
To get substring of a String in Kotlin, use String. subSequence() method.
To check if a string contains specified string in Kotlin, use String. contains() method. Given a string str1 , and if we would like to check if the string str2 is present in the string str1 , call contains() method on string str1 and pass the the string str2 as argument to the method as shown below.
The string functions are examples of what Kotlin considers Intrinsic
functions. They are defined based on the platform they are running on and you won't be able to find an implementation of them in the source code.
For the JVM they will be directly mapped to the corresponding native java.lang.String
methods. This ensures there is no runtime overhead and leverages the optimizations done in the java standard library.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With