[英]Kotlin split string into range
我需要从字符串中获取一个范围。 ; - 是一个分隔符。
因此,例如,我有字符串“10;15;1”,我需要得到从 10 到 15 的范围(忽略最后一个数字)。
预期结果:
“10;15;1”-> 10..15
所以我试着写这段代码。 我怎样才能改善它? 它看起来很糟糕而且很无用
val arr = "10;15;1".split(";").dropLast(1).map { it.toBigDecimal() }
val someRange = arr[0] .. arr[1]
如果你不关心验证,你可以这样做:
fun toRange(str: String): IntRange = str
.split(";")
.let { (a, b) -> a.toInt()..b.toInt() }
fun main() {
println(toRange("10;15;1"))
}
Output:
10..15
如果你想更加偏执:
fun toRange(str: String): IntRange {
val split = str.split(";")
require(split.size >= 2) { "str must contain two integers separated by ;" }
val (a, b) = split
return try {
a.toInt()..b.toInt()
} catch (e: NumberFormatException) {
throw IllegalArgumentException("str values '$a' and/or '$b' are not integers", e)
}
}
fun main() {
try { println(toRange("oops")) } catch (e: IllegalArgumentException) { println(e.message) }
try { println(toRange("foo;bar;baz")) } catch (e: IllegalArgumentException) { println(e.message) }
println(toRange("10;15;1"))
}
Output:
str must contain two integers separated by ;
str values 'foo' and/or 'bar' are not integers
10..15
function 非常具体,因此它不能存在于标准库中。 尽管我可以建议使用正则表达式的替代方法并在字符串格式不正确的情况下返回 null 值,但我并不反对该实现。 但它使用正则表达式。
fun rangeFrom(str: String) : ClosedRange<BigDecimal>? {
val regex = """^(\d+);(\d+);\d+$""".toRegex()
val result = regex.find(str)
return result?.destructured?.let { (fst, snd) ->
fst.toBigDecimal() .. snd.toBigDecimal()
}
}
或者您可以更新您的 function 检查split
生成的列表的长度是否>= 2
并直接使用arr[0].toBigDecimal().. arr[1].toBigDecimal
但它并没有太大的不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.