Skip to content

Commit 590e675

Browse files
committed
Book Updates
-add chapter 5.2
1 parent cc97f12 commit 590e675

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

book/chapter5.2.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
title: "Chapter 5.2"
3+
---
4+
5+
# Chapter 5.2: Strings
6+
7+
A string is also considered a collection, due to the fact it is a **collection of characters**.
8+
9+
We can perform many string operations (similar to lists) like indexing, reversing, and even joining!
10+
11+
```
12+
obj my_string = "12345";
13+
obj first_char = charat(my_string, 0); # '1'
14+
```
15+
16+
## String Operations
17+
18+
The string type actually doesn't have the ability to modify in place; i.e. it is **immutable**. For most string operations, you actually create a new copy that can be assigned to something. Reversing a string is a great example, as the string is copied, then recreated in reverse.
19+
20+
```
21+
obj forwards = "abcd";
22+
obj backwards = reverse(forwards); # 'dcba'
23+
```
24+
25+
Strings support some mathematical operands too. The `+` operand adds two strings together, and you can even use the `*` operator to multiply a string.
26+
27+
```
28+
obj hello_world = "Hello, " + "world!"; # 'Hello, world!'
29+
obj excited_happy_birthday = "Happy Birthday!" * 3; # 'Happy Birthday!Happy Birthday!Happy Birthday!'
30+
```
31+
32+
## Other String Operations
33+
34+
Just like lists, strings come with a bunch of built-in operations other than the ones listed here. A full list of available operations is in the [String Documentation](/docs/types/string#operations).

0 commit comments

Comments
 (0)