将琴弦拆分成两个Kotlin

时间:2019-01-24 14:56:26

标签: android kotlin

我有一个字符串,如果字符串超过35个,则必须将其拆分为另一个字符串。将//set err out to print to file PrintStream ps = new PrintStream("err.log"); System.setErr(ps); //cause exception for testing it String s = null; s.length(); 分别转换为var string1 = "1...38"”和var result1= "1...35之类的东西。我在考虑使用拆分,但我不知道这是否是最佳选择。

5 个答案:

答案 0 :(得分:5)

In [1]: df Out[1]: col_1 col_2 0 0 1 1 2 3 2 4 5 3 6 7 4 8 9 5 10 11 6 12 13 7 14 15 8 16 17 9 18 19 10 20 21 11 22 23 将把字符串拆分成一个列表:

In [2]: result
Out[2]: 
    col_1   col_2     pair
        2       3        0
       20      21        0
        2       3        1
       22      23        1
        4       5        2
       20      21        2
        4       5        3
       22      23        3
        6       7        4
       20      21        4
        6       7        5
       22      23        5
       16      17        6
       20      21        6
       16      17        7
       22      23        7

结果

  

[“ Hello Ther”,“ e!This is”,“ a true”,“ long strin”,“ g that I w”,“ ant split”]

答案 1 :(得分:2)

您应该使用

dropdroplast(返回字符串)

val chars = "abcdefghijklmnopqrstuvwxyzabcdefghijkl"
val result1 = chars.dropLast(3) // returns abcdefghijklmnopqrstuvwxyzabcdefghi
val result2 = chars.drop(35) // returns jkl

chunked(返回字符串列表)

chars.chunked(35)) // returns [abcdefghijklmnopqrstuvwxyzabcdefghi, jkl]

取决于您的情况

答案 2 :(得分:1)

此扩展名将为您提供与其余部分关联的一对受限字符串:

fun String.limit(max: Int): Pair<String, String> =
    if (length > max) {
        take(max) to takeLast(length - max)
    } else this to ""

一些例子:

val string1 = "onqweinalsdmuizqbwnöfasdkdasqwrwfeqewwqeweqewf" //more than 35
val string2 = "onqweinalsdmuizqbwnöfasdkdasqwrwfeq" //exactly 35
val string3= "asdqwe" //less than 35

println(string1.limit(35))    // -> (onqweinalsdmuizqbwnöfasdkdasqwrwfeq, ewwqeweqewf)
println(string2.limit(35))    // -> (onqweinalsdmuizqbwnöfasdkdasqwrwfeq, )
println(string3.limit(35))    // -> (asdqwe, )

答案 3 :(得分:1)

答案 4 :(得分:1)

如果您也可以将

chunked分成2个以上的片段也可以。但是,如果您宁愿将其最多分为两部分,第一部分具有一定长度,第二部分仅包含其余部分,则可能需要使用类似以下内容的方法(类似于s1m0nw1s answer,但)使用takesubstring

fun String.splitAtIndex(index : Int) = take(index) to substring(index)

或者,如果您想安全使用它,还可以添加一些便利性检查:

fun String.splitAtIndex(index: Int) = when {
  index < 0 -> 0
  index > length -> length
  else -> index
}.let {
  take(it) to substring(it)
}

或者,如果您更喜欢例外情况:

fun String.splitAtIndex(index: Int) = require(index in 0..length).let {
  take(index) to substring(index)
}

所有这些函数都会返回一个Pair<String, String>,您可以按以下方式对其进行处理:

"someString".splitAtIndex(5).also { (atMost5Chars, remainder) ->
   println("$atMost5Chars | remainder: $remainder")
}
"someOther".splitAtIndex(4).also { 
   (first) -> println(first) // discard the remainder... but then again you could have used also just take(4)
}

如您所写,您曾考虑使用split,并且如果手头有合适的定界符,您可能还想使用以下内容:

yourInputString.split(yourDelimiter, limit = 2)

这会将yourInputString分成两部分,其中第一部分是直到第一次出现yourDelimiter为止的所有字符串。示例:

val yourInputString = "this is a string with a delimiter | and some more information that is not necessary | with some more delimiters | | |"
yourInputString.split('|', limit = 2).also {
  (uptoFirstDelimiter, remainder) -> println("$uptoFirstDelimiter --- remainder: $remainder")
}

将打印:

this is a string with a delimiter  --- remainder:  and some more information that is not necessary | with some more delimiters | | |