Skip to content

Commit b49a73e

Browse files
authored
Merge pull request #15 from petrmvala/patch-2
Simple updates to Strings.md
2 parents 8169ec9 + be60837 commit b49a73e

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

docs/src/lecture_01/strings.md

Lines changed: 14 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. If used incorrectly, Julia with throw 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
@@ -385,6 +391,13 @@ julia> replace("Sherlock Holmes", "e" => uppercase)
385391
"ShErlock HolmEs"
386392
```
387393

394+
It is even possible to replace a whole substring:
395+
396+
```jldoctest
397+
julia> replace("Sherlock Holmes", "Holmes" => "Homeless")
398+
"Sherlock Homeless"
399+
```
400+
388401
```@raw html
389402
<div class = "exercise-body">
390403
<header class = "exercise-header">Exercise:</header><p>

0 commit comments

Comments
 (0)