Tengo tres variables:
val months: Long val days: Long val hours: Long
y quiero devolver algo como esto:
3 meses, 2 días y 5 horas
Ahora esto simplemente se traduciría como:
val str = "$months months, $days days and $hours hours"
Y si mis meses tuvieran que ser 0 y los días 1 y las horas 0 entonces sería como ' 0 meses, 1 día y 0 horas '
Pero lo que estoy buscando es "1 día" en su lugar. ¿Cómo puedo hacerlo?
Definitivamente puedo usar algún tipo de StringBuilder condicional para hacer esto, pero ¿hay algo mejor y elegante ?
¿Cómo te parece esto?
fun formatTime(months: Long, days: Long, hours: Long): String = listOf( months to "months", days to "days", hours to "hours" ) .filter { (length,_) -> length > 0L } // You can add an optional map predicate to make singular and plurals .map { (amount, label) -> amount to if(abs(amount)==1L) label.replace("s", "") else label } .joinToString(separator=", ") { (length, label) -> "$length $label" } .addressLastItem() fun String.addressLastItem() = if(this.count { it == ','} >= 1) // Dirty hack to get it working quickly this.reversed().replaceFirst(" ,", " dna ").reversed() else this
Puedes verlo funcionando aquí
Otra variante sin reemplazar, contar o invertir la lista:
fun formatTime(months: Long, days: Long, hours: Long): String { val list = listOfNotNull( months.formatOrNull("month", "months"), days.formatOrNull("day", "days"), hours.formatOrNull("hour", "hours"), ) return if (list.isEmpty()) "all values <= 0" else listOfNotNull( list.take(list.lastIndex).joinToString().takeIf(String::isNotEmpty), list.lastOrNull() ).joinToString(" and ") } fun Long.formatOrNull(singular: String, plural: String = "${singular}s") = when { this == 1L -> "$this $singular" this > 1L -> "$this $plural" else -> null }
También tiene un respaldo si todos los valores son <= 0... también puede usar una cadena vacía o lo que prefiera.
Si no le gusta que se creen listas intermedias para concatenar la cadena, también puede usar algo de la siguiente manera en la ruta else:
list.iterator().run { buildString { while (hasNext()) { val part = next() if (length > 0) if (hasNext()) append(", ") else append(" and ") append(part) } } }