Skip to content

Commit 4a9d906

Browse files
committed
Update string interpolation docs with further information on escaping symbols
1 parent daa2fc2 commit 4a9d906

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

_overviews/core/string-interpolation.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,23 @@ String interpolators can also take arbitrary expressions. For example:
4242

4343
will print the string `1 + 1 = 2`. Any arbitrary expression can be embedded in `${}`.
4444

45+
For some special characters, it is necessary to escape them when embedded within a string.
4546
To represent an actual dollar sign you can double it `$$`, like here:
4647

4748
println(s"New offers starting at $$14.99")
4849

4950
which will print the string `New offers starting at $14.99`.
5051

52+
Double quotes also need to be escaped. This can be done on an individual basis using a backslash before the double quote, as follows:
53+
54+
val person = "{\"name\":\"James\"}"
55+
56+
On longer strings, this may be time intensive. To avoid the need to escape every individual quote, triple quotes can be used as shown:
57+
58+
val person = """{"name":"James"}"""
59+
60+
Both of these strings, when printed, will produce the string `{"name":"James"}`.
61+
5162
### The `f` Interpolator
5263

5364
Prepending `f` to any string literal allows the creation of simple formatted strings, similar to `printf` in other languages. When using the `f`

0 commit comments

Comments
 (0)