<< Back to SCALA
Scala Strip Strings: stripLineEnd and stripMargin
Use the stripLineEnd and stripMargin functions to remove characters from strings.Strip. Often strings have surrounding whitespace. A string may have a trailing newline. It may have a margin (with a separator) at its start.
With special strip functions, on the StringLike type, we process these whitespace sequences. We eliminate them. We handle prefixes and suffixes.
StripLineEnd. This program invokes the stripLineEnd def. It removes a trailing newline from a string. It is similar to "chop" or "chomp" in scripting languages.
Scala program that uses stripLineEnd
val name = "Thomas Browne\n"
// Remove end newline from the string.
val result = name.stripLineEnd
// Print the result.
println("[" + result + "]")
Output
[Thomas Browne]
StripMargin. Often in text processing a string has a "start" character that ends its leading margin. Whitespace chars (like spaces) may precede this.
Here: With stripMargin we handle this case. We eliminate the entire margin (leading blanks and separator) from the strings in a list.
Scala program that uses stripMargin
// These strings have margins.
val lines = List(" :cat", " :dog")
for (line <- lines) {
// Remove all blanks and a separator character at start of string.
val result = line.stripMargin(':')
// Print results.
println("[" + line + "] " + result)
}
Output
[ :cat] cat
[ :dog] dog
StripMargin, no argument. We can use stripMargin with no argument. This removes a vertical bar from the beginning of the string (along with the spaces and whitespace).
Scala program that uses stripMargin
val data = " |Example"
// With this version of stripMargin, the vertical bar is removed.
val result = data.stripMargin
// Print the result.
println("[" + result + "]")
Output
[Example]
StripPrefix, stripSuffix. These methods are similar to startsWith and endsWith, but they also remove the starting and ending substrings.
Info: If a string's start or end matches the argument, that part is removed. Otherwise the string is returned with no changes.
Scala program that uses stripPrefix and stripSuffix
// Has a prefix and a suffix (content is "cat").
val input = "x:cat:end"
// Remove prefix.
val prefixRemoved = input.stripPrefix("x:")
// Remove suffix.
val suffixRemoved = input.stripSuffix(":end")
// Remove prefix and suffix.
val bothRemoved = input.stripPrefix("x:").stripSuffix(":end")
// Print stripped strings.
println(prefixRemoved)
println(suffixRemoved)
println(bothRemoved)
Output
cat:end
x:cat
cat
A review. Magic is great. But many parts of programming are not magic. Instead we must clear unwanted characters (like newlines, spaces) from strings. Strip methods help.
Related Links:
- Scala Convert: String to Int, List and Array
- Scala Odd and Even Numbers
- Scala Regex, R Examples: findFirstMatchIn
- Scala Interview Questions (2021)
- Scala Class Examples: Trait Keyword
- Scala indexOf: Lists and Strings
- Scala Int Examples: Max and Min
- Scala 2D List, Array Examples: Array.ofDim
- Scala Def: return Unit and Lambdas
- Scala Initialize List: List.newBuilder, List.empty
- Scala Option: None, get and getOrElse
- Scala Exception Handling: Try, Catch and Throw
- Scala Map Examples
- Scala String Examples: StringLike
- Scala Match and Case Examples
- Scala Remove Duplicates From List: Distinct Example
- Scala If, Else Example (Match Case If)
- Scala Console: println, printf and readLine
- Scala Strip Strings: stripLineEnd and stripMargin
- Scala Vector Examples
- Scala Var and Val (Variables and Constants)
- Learn Scala Tutorial
- Scala Keywords
- Scala format and formatted Strings
- Scala List Examples
- Scala StringBuilder Examples
- Scala Set Examples: contains, intersect
- Scala Tuple Examples (Return Multiple Values)
- Scala For to, until, while Loop Examples
- Scala Slice: Substring, List Slice
- Scala Sorted List Examples: Ordering.Int
- Scala Split String Examples
- Scala Array Examples: Apply and Update
- Scala Range Examples: Start, End and Step
Related Links
Adjectives
Ado
Ai
Android
Angular
Antonyms
Apache
Articles
Asp
Autocad
Automata
Aws
Azure
Basic
Binary
Bitcoin
Blockchain
C
Cassandra
Change
Coa
Computer
Control
Cpp
Create
Creating
C-Sharp
Cyber
Daa
Data
Dbms
Deletion
Devops
Difference
Discrete
Es6
Ethical
Examples
Features
Firebase
Flutter
Fs
Git
Go
Hbase
History
Hive
Hiveql
How
Html
Idioms
Insertion
Installing
Ios
Java
Joomla
Js
Kafka
Kali
Laravel
Logical
Machine
Matlab
Matrix
Mongodb
Mysql
One
Opencv
Oracle
Ordering
Os
Pandas
Php
Pig
Pl
Postgresql
Powershell
Prepositions
Program
Python
React
Ruby
Scala
Selecting
Selenium
Sentence
Seo
Sharepoint
Software
Spellings
Spotting
Spring
Sql
Sqlite
Sqoop
Svn
Swift
Synonyms
Talend
Testng
Types
Uml
Unity
Vbnet
Verbal
Webdriver
What
Wpf