Extension function of String class

kotlin string extension function example
kotlin generic extension function
kotlin extension function inside class
kotlin extension function in java
kotlin extension function best practices
kotlin extension function override
kotlin extension android
kotlin static extension function

I want to create an extension function of String which takes a String and returns a new String with the characters as the first, but sorted in ascending order. How can I do this? I am new to Kotlin.

fun String.ascending() = String(toCharArray().sortedArray())

Then:

println("14q36w25e".ascending())  // output "123456eqw"

Extension function of String class, This will define a new function called escapeForXml as an extension to the String class, allowing us to call it as described above. Inside this  Basically, an extension function is a member function of a class that is defined outside the class. For example, you need to use a method to the String class that returns a new string with first and last character removed; this method is not already available in String class. You can use extension function to accomplish this task.

You can combine built in extensions to do it quickly:

fun String.sortedAlphabetically() = toCharArray().apply { sort() }

First you get array of underlying characters, then you apply sort to that array and return it. You are free to cast result .toString() if You need.

Extensions, class AB(val name: String){. override fun toString(): String {. return "Name is $​name". } } fun main(){. // An extension function as a nullable receiver. fun AB?.​output  Extending the string class Although the object extension is perhaps a bit type unsafe, you can extend a specific class called myclass just by using this myclass . Below we extended the string class with this string.

Extension function for printing characters of a string in ascending order

Way 1:

fun String.sortStringAlphabetically() = toCharArray().sortedArray())

Way 2:

fun String.sortStringAlphabetically() = toCharArray().sortedArrayDescending().reversedArray()

Way 3:

fun String.sortStringAlphabetically() = toCharArray().sorted().joinToString(""))

Way 4:

fun String.sortStringAlphabetically() = toCharArray().sortedBy{ it }.joinToString(""))

Then you can use this extension function by the below code:

fun main(args: Array<String>) {
    print("41hjhfaf".sortStringAlphabetically())
}

Output: 14affhhj

Extension Methods, An extension function in Kotlin allows to add behavior to an existing class, This is a very common Java practice */ class StringUtil { fun  Since you cannot extend string.Trim(). You could make an Extension method as described here that trims and reduces whitespace. namespace CustomExtensions { //Extension methods must be defined in a static class public static class StringExtension { // This is the extension method.

I have one more:

fun String.inAscending(): String = toMutableList().sortedBy { it }.joinToString("")

And better:

fun String.ascending(): String = toMutableList().sorted().joinToString("")

Extension Methods in Kotlin, Kotlin provides the ability to extend a class with new functionality without having to inherit To declare an extension function, we need to prefix its name with a receiver type, i.e. the type being extended or the class name. toString(): String { <Extension()> _ Public Function MethodName(ByVal InputValue As String) As String End Function. The above shows the complete function. This function is an extension of the public System.String method. This is because the input value is also a String. So now, all we need to do is add some functionality to this module (or class) and use it in our project.

In my opinion, do it in kotlin way i.e

fun String.sortAscending() = toCharArray().sortedBy{ it }.joinToString("")

Kotlin extension function, The first String is called the receiver of the function. Ok, we saw that Extension Functions can be applied to any class. But can we do something  This will define a new function called escapeForXml as an extension to the String class, allowing us to call it as described above. Inside this function, we can access the receiver using this, the same as if we had written this inside the String class itself:

Kotlin Extension Functions, methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods defined inside data types in the current namespace or any one of its parents, with child namespaces having higher precedence than parent namespaces. Extension methods defined inside any type imports in the current file. Extension methods defined inside any namespace imports in the current file.

Extensions in Kotlin · Suneet Agrawal, Which feature of Kotlin helps to extend a class with new functionality without having to inherit from the class? This example prints " Shape ", because the extension function being called depends only on the declared type of the parameter s, which is the Shape class. If a class has a member function, and an extension function is defined which has the same receiver type, the same name, and is applicable to given arguments,

[Kotlin Pearls 6] Extensions: The Good, The Bad and The Ugly, Why do extension methods need to be in a static class? The class String includes methods for examining individual characters of the sequence, for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase.

Comments
  • why do you use the apply ? why not toCharArray().sort() directly ?
  • sort() returns Unit, apply returns receiver object (CharArray).