String Interpolation:
String interpolation (or variable interpolation, variable substitution, or variable expansion) is the process of evaluating a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values.
Note:
Scala provides three string interpolation methods out of the box: s, f and raw.
1)String interpolation to print a variable:
String interpolation (or variable interpolation, variable substitution, or variable expansion) is the process of evaluating a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values.
Note:
Scala provides three string interpolation methods out of the box: s, f and raw.
1)String interpolation to print a variable:
- prefix the s at the beginning of our println statement.
- use the dollar sign $ to refer to our variable.
Example:
2)String interpolation on object properties:
To access the properties inside the object, wrap our expression inside curly braces
Example:
Note:
case class as an alternative to creating Plain Old Java Objects (POJO) but without having to write boilerplate code for getters and setters.
3)String interpolation to evaluate expressions:
String interpolation with expressions by using the curly braces
Example:
4)String interpolation for formatting text
Format your strings by say pre-pending some white spaces in front of the text.
Example:
5)f interpolation to format numbers:
if we wanted to print the 3 decimal places for the cost variable. This can be achieved by using the f interpolator.
Example:
6)Raw interpolation:
The raw String interpolation will allow you to print any symbols within your String
Example:
Escape Characters And Create Multi-line String:
Let's assume JSON string as source
1)escape a Json String using backslash(\):
Example: with default indent pipe(|):
Example: with custom indent #:
Let's assume JSON string as source
1)escape a Json String using backslash(\):
- print a JSON String by making use of backslash \
- But it does not apply to all the situations , let's assume, if string is too larger, so having backslack typed is bad approach.
Example:
2)escape a Json String using triple quotes(""" """):
Scala's better solution is to have triple quotes instead backslash around the JSON string
Syntax:
"""<YOUR TEXT TO ESCAPE>"""
3)Creating multi-line text using stripMargin:
- To indent text , that is more readable using stripMargin.
- By defualt pipe (|) is the indent, stripMargin allows to specify the different characters as well
stringMargin('#')
Example: with default indent pipe(|):
Example: with custom indent #:
No comments:
Post a Comment