Skip to content

Commit 9dd5c51

Browse files
authored
Additions to string escaping
1 parent 8169ec9 commit 9dd5c51

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

docs/src/lecture_01/strings.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ julia> str[1:1] # returns the first character as String
5858
"H"
5959
```
6060

61-
When using strings, we have to pay attention to some special characters, specifically to the following three characters: `\`, `"` and `$`. If we want to use any of these three characters, we have to use a backslash before them. The reason is that these characters have a special meaning. For example, if we use a quote inside a string, then the rest of the string will be interpreted as a Julia code and not a string.
61+
When using strings, we have to pay attention to following characters with special meaning: `\`, `"` and `$`. In order to use them as regular characters, they need to be escaped with a backslash (`\`). For example, unescaped double quote (`"`) would end the string prematurely, forcing the rest being interpreted as Julia code. This is a common malicious attack vector called [code injection](https://en.wikipedia.org/wiki/Code_injection).
6262

6363
```jldoctest strings
6464
julia> str1 = "This is how a string is created: \"string\"."
@@ -72,6 +72,12 @@ julia> str2 = "\$\$\$ dollars everywhere \$\$\$"
7272
"\$\$\$ dollars everywhere \$\$\$"
7373
```
7474

75+
```jldoctest strings
76+
julia> "The $ will be fine."
77+
ERROR: syntax: invalid interpolation syntax: "$ "
78+
```
79+
80+
No, they won't. Not escaping characters will result in an error.
7581
Printing of strings can be done by the `print` function or the `println` function that also add a new line at the end of the string.
7682

7783
```jldoctest strings

0 commit comments

Comments
 (0)