Trimming strings in Scala

前端 未结 5 1960
无人共我
无人共我 2020-12-25 10:19

How do I trim the starting and ending character of a string in Scala

For inputs such as \",hello\" or \"hello,\", I need the output as

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-25 10:58

    To trim the start and ending character in a string, use a mix of drop and dropRight:

    scala> " hello,".drop(1).dropRight(1)

    res4: String = hello

    The drop call removes the first character, dropRight removes the last. Note that this isn't "smart" like trim is. If you don't have any extra character at the start of "hello,", you will trim it to "ello". If you need something more complicated, regex replacement is probably the answer.

提交回复
热议问题