score:172
How about using dropRight, which works in 2.8:-
"abc!".dropRight(1)
Which produces "abc"
score:2
If you want just to remove the last character use .dropRight(1)
. Alternatively, if you want to remove a specific ending character you may want to use a match pattern as
val s: String = "hello!"
val sClean: String = s.takeRight(1) match {
case "!" => s.dropRight(1)
case _ => s
}
score:4
string.reverse.substring(1).reverse
That's basically chop, right? If you're longing for a chop method, why not write your own StringUtils
library and include it in your projects until you find a suitable, more generic replacement?
Hey, look, it's in commons.
score:5
If you want the most efficient solution than just use:
str.substring(0, str.length - 1)
score:7
val str = "Hello world!"
str take (str.length - 1) mkString
score:13
string.init // padding for the minimum 15 characters
Source: stackoverflow.com
Related Query
- Remove Characters from the end of a String Scala
- How to remove quotes from front and end of the string Scala
- Move characters to the end of a string in scala
- Spark - Scala Remove special character from the beginning and end from columns in a dataframe
- Scala remove characters from String following a sequence
- Scala - Get last two characters from string
- What is the proper way to remove elements from a scala mutable map using a predicate
- remove the filtered line from the file using scala fs2 file streaming
- How to conditionally remove the first two characters from a column
- Remove words of a string started by uppercase characters in Scala
- need to remove first line from a string and then first two word in scala
- How do I conditionally remove text from a string in a column in a Scala dataframe?
- How to extract a specific text from the string variable in scala
- Scala - List from the end (problem with commas)
- how to remove every nth element from the Scala list?
- How to limit the number of characters in a string displayed using Scala
- Mapping the values from a Map to a String in Scala
- Get character from a string in Scala but keep the result as a string?
- Replace characters from string using scala
- How remove substring from a particular character in string in scala
- Scala - Remove all elements in a list/map of strings from a single String
- Find Nth element from the end in Scala using recursive tail
- Scala String indexOf Character from the Right Side
- Obtaining the value from any key-value from a string representation of a json in Scala (using scala.util.parsing.json)
- How to delete Duplicate Characters from a given region in String?(given that we have the buffer from which we read the string from)
- Constant time appending and retrieval from the end with Scala
- How to remove all characters that start with "_" from a spark string column
- How to find the length of characters in a string using Scala Spark
- How to remove List() and escape characters from response JSON in Scala
- Splitting String in scala ignores void fields at the end
More Query from same tag
- Scala - how to initialize object with data from another class
- DataFrame persist does not improve performance in Spark
- Scala list validSelectValues not updated
- Scala brackets java.lang.StackOverflowError
- Looking for ways to optimize the program [Scala/Spark]
- String interpolator with variable
- spark streaming foreach multiple writer
- serverless - AWS Lambda scala example
- Is this idiom Scala. Making a def to shorten a statement?
- RDD of List String convert to Row
- traverse collection of type "Any" in Scala
- Can objects be implemented in terms of higher order functions?
- Parsing Objects in non-standard format
- java/scala web application and psexec
- How does one "override" an inner class in Scala?
- PlayFramework FakeRequest returns 400 error
- Error while running UDF on dataframe columns
- Is Either.right = Right and Either.Left=Left?
- Transform an IgniteCursor[T]/Iterable[T] to Future[Seq[T]]
- Scala/Spark : How to do outer join based on common columns
- Why does this list matcher not working as expected?
- Generic builder of (almost) identical 3rd party classes
- Argonaut.io: how to rename json property for Right/Left in case class containing Either
- How to convert Binary to String in scala glue
- RDD taken from spark DStream is not getting distributed between partitions
- Scala test case with URL
- Is finally "out of scope" in a try/catch block
- object play.http.HttpEntity.Streamed is not a value
- LinkedList: Iterate and remove element
- How to select max, min in same query in slick