format

inline fun String.format(vararg args: Any?): String(source)

Uses this string as a format string and returns a string obtained by substituting format specifiers in the format string with the provided arguments, using the default locale.

See java.util.Formatter class documentation for the syntax of format specifiers for the format string.

Since Kotlin

1.0

Samples

import java.util.Locale
import kotlin.test.*

fun main() { 
   //sampleStart 
   // format negative number in parentheses
val negativeNumberInParentheses = "%(d means %1\$d".format(-31416)
println(negativeNumberInParentheses) // (31416) means -31416 
   //sampleEnd
}

inline fun String.Companion.format(format: String, vararg args: Any?): String(source)

Uses the provided format as a format string and returns a string obtained by substituting format specifiers in the format string with the provided arguments, using the default locale.

See java.util.Formatter class documentation for the syntax of format specifiers for the format string.

Since Kotlin

1.0

Samples

import java.util.Locale
import kotlin.test.*

fun main() { 
   //sampleStart 
   // format negative number in parentheses
val negativeNumberInParentheses = String.format("%(d means %1\$d", -31416)
println(negativeNumberInParentheses) // (31416) means -31416 
   //sampleEnd
}

inline fun String.format(locale: Locale?, vararg args: Any?): String(source)

Uses this string as a format string and returns a string obtained by substituting format specifiers in the format string with the provided arguments, using the specified locale. If locale is null then no localization is applied.

See java.util.Formatter class documentation for the syntax of format specifiers for the format string.

Since Kotlin

1.4

Samples

import java.util.Locale
import kotlin.test.*

fun main() { 
   //sampleStart 
   // format with German conventions
val withGermanThousandsSeparator = "%,d".format(Locale.GERMANY, 12345)
println(withGermanThousandsSeparator) // 12.345
// 12.345

// format with US conventions
val withUSThousandsSeparator = "%,d".format(Locale.US, 12345)
println(withUSThousandsSeparator) // 12,345 
   //sampleEnd
}

inline fun String.Companion.format(locale: Locale?, format: String, vararg args: Any?): String(source)

Uses the provided format as a format string and returns a string obtained by substituting format specifiers in the format string with the provided arguments, using the specified locale. If locale is null then no localization is applied.

See java.util.Formatter class documentation for the syntax of format specifiers for the format string.

Since Kotlin

1.4

Samples

import java.util.Locale
import kotlin.test.*

fun main() { 
   //sampleStart 
   // format with German conventions
val withGermanThousandsSeparator = String.format(Locale.GERMANY, "%,d", 12345)
println(withGermanThousandsSeparator) // 12.345

// format with US conventions
val withUSThousandsSeparator = String.format(Locale.US, "%,d", 12345)
println(withUSThousandsSeparator) // 12,345 
   //sampleEnd
}