Remove characters from the end of a Scala line

What is the easiest way to remove the last character from the end of a line in Scala?

I believe the Rubys String class has very useful methods like chop. I would use "oddoneoutz" .headOption in Scala, but it has depreciated. I do not want to go into complex complexity:

string.slice(0, string.length - 1) 

Please tell me that there is a simple simple method, for example, chopping off something so common.

+46
string scala
Nov 30 '09 at 21:23
source share
5 answers

How about using dropRight, which works in 2.8: -

 "abc!".dropRight(1) 

What makes "abc"

+90
Nov 30 '09 at 22:09
source share
 string.init // padding for the minimum 15 characters 
+10
Dec 01 '09 at 3:10
source share
 val str = "Hello world!" str take (str.length - 1) mkString 
+7
Nov 30 '09 at 22:09
source share
 string.reverse.substring(1).reverse 

What basically hack, right? If you crave a shredding method, why not write your own StringUtils library and include it in your projects until you find a suitable replacement for a more general one?

Hey look, that’s in general.

Apache Commons StringUtils .

+4
Nov 30 '09 at 21:30
source share

If you want the most effective solution than just using:

 str.substring(0, str.length - 1) 
0
Dec 20 '17 at 2:04 on
source share



All Articles