Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can't understand source code implementation of string.kt in kotlin

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?

like image 655
Jim Green Avatar asked Jun 13 '17 13:06

Jim Green


People also ask

How do I get text from String in Kotlin?

To get substring of a String in Kotlin, use String. subSequence() method.

How do I check if a String is value in Kotlin?

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.


1 Answers

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.

like image 174
Kiskae Avatar answered Oct 07 '22 18:10

Kiskae