2

There is a recursive function that takes a string as input. It breaks this string into parts and calls itself for each part. This function does not change the input string, only reads it. The function is implemented in Kotlin. To create a substring, the substring function is called:

    myFunc(str.substring(begin, length))

There are doubts about the effectiveness of this implementation. A new string is created for each call, although it is enough to pass the beginning and length of the substring in the original string. Does Kotlin have a class for substrings? For example, in the C ++ standard library there is a class std :: string_view, and in Qt there is a class QStringRef. Is there something similar in Kotlin?

Or maybe the String class already optimized and the substring function does not allocate new memory, and the new instance uses the same buffer in memory as the original?

Andrey Epifantsev
  • 976
  • 1
  • 11
  • 27
  • You may be looking for a data structure more like a Rope than a string. Ropes are CharSequences that are constructed from multiple strings or views on strings. Inserts and deletes are modelled as views, not modifications. There is an open source implementation here: http://ahmadsoft.org/ropes/ – time4tea Dec 28 '21 at 19:08

2 Answers2

3

You can look at the source code yourself to check.

In the case of java.lang.String (used by Kotlin/JVM) it was fairly simple until Java 7: a String had a reference to a character array, along with start and end indices. So while a substring must be a new String instance, it had a reference to the same character array as the String from which it was generated. In other words, it optimised the substring case.

However, the String class is now more complex, and now seems to be optimised more for heap usage: see this question.

If you're really concerned about the performance of this particular method, you could dispense with creating objects entirely, and pass in the original String and your own start and end values. However, the usual rules about optimisation apply: it's only worth complicating your code like this if it's in a hotspot — and you'll need to do some testing to show whether it is, and whether your rewrite actually improves things.

gidds
  • 16,558
  • 2
  • 19
  • 26
-1

If any method is not in the standard library function, you can create Extention function to provide more rich support.

Example: Remove First and Last Character of String

fun String.removeFirstLastChar(): String =  this.substring(1, this.length - 1)

fun main(args: Array<String>) {
    val myString= "Hello Everyone"
    val result = myString.removeFirstLastChar()
    println("First character is: $result")
}
When y

ou run the program, the output will be:

First character is: ello Everyon

Here, an extension function removeFirstLastChar() is added to the String class.

The class name is the receiver type (String class in our example). The this keyword inside the extension function refers the receiver object.

Pawan Soni
  • 860
  • 8
  • 19