althaf
04/07/2022, 7:21 AMspannableAmount = SpannableString(buildString {
currency?.takeIf { it.isNotEmpty() }?.let { currency ->
currencyLength1 = currency.length
append("$currency")
}
amount?.takeIf { it.isNotEmpty() }?.let { amount ->
amountLength1 = currencyLength1 + amount.length
append(" $amount")
}
ratePercent?.takeIf { it.isNotEmpty() }?.let { rate ->
rateLength1 = amountLength1 + rate.length
append(" $ratePercent")
}
})
ildar.i [Android]
04/07/2022, 8:01 AMalthaf
04/07/2022, 8:14 AMildar.i [Android]
04/07/2022, 8:19 AMildar.i [Android]
04/07/2022, 8:27 AMfun main() {
val currency = "eur"
val amount = "1000"
val ratePercent = "10"
var currencyLength1 = 0
var amountLength1 = 0
var rateLength1 = 0
val spannableAmount = buildString {
currency?.takeIf { it.isNotEmpty() }?.let { currency ->
currencyLength1 = currency.length
append("$currency")
}
amount?.takeIf { it.isNotEmpty() }?.let { amount ->
amountLength1 = currencyLength1 + amount.length
append(" $amount")
}
ratePercent?.takeIf { it.isNotEmpty() }?.let { rate ->
rateLength1 = amountLength1 + rate.length
append(" $ratePercent")
}
}
println(spannableAmount.toString())
println(rateLength1)
}
Adrijan Rogan
04/07/2022, 9:42 AMif (currency != null && currency.isNotEmpty()) {
currencyLength1 = currency.length
append(currency)
}
2. Nit: instead of using append(" $amount")
, use two appends -- append(" ")
and append(amount)
-- to avoid additional string construction