In addition to extension String.format introduce String.Companion.format(format, args) to be used like in java.

This commit is contained in:
Ilya Gorbunov
2016-02-03 06:02:09 +03:00
parent 7105c7c182
commit c243a2bdd5
2 changed files with 20 additions and 2 deletions
@@ -122,6 +122,13 @@ public inline fun String.toCharArray(destination: CharArray, destinationOffset:
@kotlin.internal.InlineOnly
public inline fun String.format(vararg args: Any?): String = java.lang.String.format(this, *args)
/**
* Uses this string as a format string and returns a string obtained by substituting the specified arguments,
* using the default locale.
*/
@kotlin.internal.InlineOnly
public inline fun String.Companion.format(format: String, vararg args: Any?): String = java.lang.String.format(format, *args)
/**
* Uses this string as a format string and returns a string obtained by substituting the specified arguments, using
* the specified locale.
@@ -129,6 +136,13 @@ public inline fun String.format(vararg args: Any?): String = java.lang.String.fo
@kotlin.internal.InlineOnly
public inline fun String.format(locale: Locale, vararg args : Any?) : String = java.lang.String.format(locale, this, *args)
/**
* Uses this string as a format string and returns a string obtained by substituting the specified arguments,
* using the default locale.
*/
@kotlin.internal.InlineOnly
public inline fun String.Companion.format(locale: Locale, format: String, vararg args: Any?): String = java.lang.String.format(locale, format, *args)
/**
* Splits this char sequence around matches of the given regular expression.
+6 -2
View File
@@ -67,10 +67,14 @@ class StringJVMTest {
@test fun formatter() {
assertEquals("12", "%d%d".format(1, 2))
assertEquals("12", String.format("%d%d", 1, 2))
assertEquals("1,234,567.890", "%,.3f".format(Locale.ENGLISH, 1234567.890))
assertEquals("1.234.567,890", "%,.3f".format(Locale.GERMAN, 1234567.890))
assertEquals("1 234 567,890", "%,.3f".format(Locale("fr"), 1234567.890))
assertEquals("1.234.567,890", "%,.3f".format(Locale.GERMAN, 1234567.890))
assertEquals("1 234 567,890", "%,.3f".format(Locale("fr"), 1234567.890))
assertEquals("1,234,567.890", String.format(Locale.ENGLISH, "%,.3f", 1234567.890))
assertEquals("1.234.567,890", String.format(Locale.GERMAN, "%,.3f", 1234567.890))
assertEquals("1 234 567,890", String.format(Locale("fr"), "%,.3f", 1234567.890))
}
@test fun toByteArrayEncodings() {