Trim a string based on the string length

后端 未结 12 863
無奈伤痛
無奈伤痛 2020-12-12 14:29

I want to trim a string if the length exceeds 10 characters.

Suppose if the string length is 12 (String s=\"abcdafghijkl\"), then the new trimmed string

12条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-12 15:00

    Here is the Kotlin solution

    One line,

    if (yourString?.length!! >= 10) yourString?.take(90).plus("...") else yourString
    

    Traditional,

    if (yourString?.length!! >= 10) {
      yourString?.take(10).plus("...")
     } else {
      yourString
     }
    

提交回复
热议问题