Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Kotlin Dictionary

  1. Home
  2. Kotlin Dictionary
  3. String.split() / joinToString()

String.split() / joinToString()

Functions for splitting a string by a delimiter, or joining the elements of a collection into a string with a separator.

Syntax

string.split(delimiter, limit = maxSplits)

collection.joinToString(separator = separator, prefix = prefix, postfix = postfix)

Method List

MethodDescription
split(vararg delimiters: String)Splits the string by the specified string delimiters and returns a List<String>.
split(vararg delimiters: Char)Splits the string by the specified characters and returns a List<String>.
joinToString(separator, prefix, postfix, limit, truncated, transform)Joins the elements of a collection into a string. All parameters are optional.
joinTo(buffer, separator, ...)Appends the joined result to an existing Appendable.

Sample Code

sample_string_split_join.kt
fun main() {
    // Split by comma.
    val csv = "item_a,item_b,item_c,item_d"
    val parts = csv.split(",")
    println(parts) // [item_a, item_b, item_c, item_d]

    // Limit the number of splits.
    val limited = csv.split(",", limit = 2)
    println(limited) // [item_a, item_b,item_c,item_d]

    // Split by multiple delimiters.
    val text = "one two,three;four"
    val words = text.split(" ", ",", ";")
    println(words) // [one, two, three, four]

    // Join a list into a string.
    val list = listOf("item_a", "item_b", "item_e")
    val joined = list.joinToString(separator = " / ")
    println(joined) // item_a / item_b / item_e

    // Add a prefix and postfix.
    val withBrackets = list.joinToString(separator = ", ", prefix = "[", postfix = "]")
    println(withBrackets) // [item_a, item_b, item_e]

    // Join with a transformation applied to each element.
    val numbers = listOf(1, 2, 3, 4, 5)
    val result = numbers.joinToString(separator = " + ", transform = { it.toString() })
    println(result) // 1 + 2 + 3 + 4 + 5
}

The command looks like this:

kotlinc string_split_join.kt -include-runtime -d string_split_join.jar
java -jar string_split_join.jar
[item_a, item_b, item_c, item_d]
[item_a, item_b,item_c,item_d]
[one, two, three, four]
item_a / item_b / item_e
[item_a, item_b, item_e]
1 + 2 + 3 + 4 + 5

Notes

split() splits a string by a delimiter and returns a List<String>. To use a regular expression, pass a Regex object as the argument. If delimiters appear consecutively, empty strings will be included in the result list. To exclude them, use filter { it.isNotEmpty() }.

joinToString() joins the elements of a collection into a single string. All parameters have default values, so calling it without arguments joins elements with a comma separator. Use limit to cap the number of elements included; any excess is replaced with the value of truncated (default: "...").

If you find any errors or copyright issues, please .