Scala: String Chomp

Does Scala have an API to execute "chomp" in a String? Preferably, I would like to convert the string "abcd \ n" to "abcd"

Thanks Ajay

+5
source share
4 answers

There is java.lang.String.trim() , but it also removes leading spaces. There is also RichString.stripLineEnd , but that only removes \n and \r .

+11
source

If you do not want to use Apache Commons Lang, you can scroll your own, according to these lines.

 scala> def chomp(text: String) = text.reverse.dropWhile(" \n\r".contains(_)).reverse chomp: (text: String)String scala> "[" + chomp(" ab cd\r \n") + "]" res28: java.lang.String = [ ab cd] 
+4
source

Actually there is box support for chomp 1

 scala> val input = "abcd\n" input: java.lang.String = abcd scala> "[%s]".format(input) res2: String = [abcd ] scala> val chomped = input.stripLineEnd chomped: String = abcd scala> "[%s]".format(chomped) res3: String = [abcd] 

1 for some definition of chomp; really the same answer as sepp2k but showing how to use it on String

+4
source

Why not use Apache Commons Lang and StringUtils.chomp () ? One of the great features of Scala is that you can use existing Java libraries.

+1
source

All Articles