String is the Java String class. String literals are enclosed with double quotes (
"). Multi-line string literals are enclosed in triple double quotes:
val s = """This is a multi-line string"""
The above string will have empty space at the beginning of 2nd and 3rd line. To strip them, use
| (or any character by passing it into
stripMargin method) as follows:
val s = """This is |a multi-line |string""".stripMargin
Multi-line string literal also escapes
StringOps offers more methods on
String via implicit conversion, such as treating a
String as a sequence of characters in a
for loop or use
map on them. To explicitly convert a
String object to a collection type, use
s to any string literal allows the usage of variables directly in the string.
s"Hello, $name" interpolates the string with variable
name. String interpolators can also take arbitrary expressions, and they should be enclosed in
If the string contains escaped double quotes, use triple quotes instead:
s """This is a quote: \"$quote\"""".
printf style formatting
f to any string literal allows the creation of simple formatted strings, similar to printf in other languages. When using the
f interpolator, all variable references should be followed by a
printf-style format string, like
%d. For example,
f"$name%s is $height%2.2f meters tall".
f interpolator is typesafe. If you try to pass a format string that only works for integers but pass a double, the compiler will issue an error.
raw interpolator is similar to the
s interpolator except that it performs no escaping of literals within the string.
capitalize: convert a string to upper case.
split: takes a String, a Char, or regular expressions and splits the string.
replaceAll: It replaces all occurrences (can be specified with regex) with another string.
replaceFirstdoes the same for first occurrence only.
scala.util.matching.Regex class provides the functionality. To construct a regex object, usr
r method of the String class. You need to use raw string
"""...""" to avoid escaping backslashes:
val pattern = """\s+[0-9]+\s+""".r
pattern.findAllIn(str)to find all matches in
str. It returns an iterator.
pattern.findFirstIn(str)to find the first match and return an
pattern.findPrefixOf(str)to check whether the beginning of the given string matches.
replace methods (such as
replaceAllIn) to replace all matches in a given string with another string:
Extract matched patterns
Use groups to extract subexpressions from matches. Put parentheses around the subexpression to form a group. For example,
val pattern = "([0-9+]) ([a-z]+)".r
val pattern(num, item) = str assign
num to match of
item to match of
[a-z]+. You can think of this as