-
Notifications
You must be signed in to change notification settings - Fork 33
/
basic_string_example.odin
86 lines (67 loc) · 2.35 KB
/
basic_string_example.odin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package basic_string_example
import "core:fmt"
import "core:strings"
/*
Strings in Odin are immutable.
Runes are unencoded code points like 0x96EA which get viewed as 雪.
When you construct a string with runes, they get encoded into a UTF-8 format and stored as an array of bytes.
You can think of runes as characters, but be careful, as one rune does not always equal one character.
For example: 👋🏻 produces 2 runes. One for the hand and one for the mask color.
*/
main :: proc() {
name1 := "雪"
name2 := "月"
// Check if the names are equal.
is_equal := strings.compare(name1, name2)
if is_equal == 0 {
fmt.println("The names match!")
} else {
fmt.println("The names do not match!")
}
// contains_rune will return true if the rune is present, false otherwise.
contains_A := strings.contains_rune(name1, 'A')
if contains_A {
fmt.println("name_1 contains the rune!")
} else {
fmt.println("name_1 does not contain the rune!")
}
// index will return the index of the needle, or -1 if not present
index_of_A := strings.index(name1, "A")
if index_of_A == -1 {
fmt.println("name_1 does not contain the needle!")
} else {
fmt.println("name_1 contains the needle and is located at index:", index_of_A)
}
fmt.println("name1 is", strings.rune_count(name1), "rune(s) long.")
// Join the two names together separated by a comma!
list_of_names := strings.join({name1, name2}, ",")
defer delete(list_of_names)
fmt.println(list_of_names)
// Split the list of names into an array of names.
names := strings.split(list_of_names, ",")
defer delete(names)
fmt.println(names)
// Concatenate strings.
new_name := strings.concatenate({name1, name2})
defer delete(new_name)
fmt.println(new_name)
FILE_CONTENTS ::
`README.md
.gitignore
chapter_1.md`
file_names := strings.split(FILE_CONTENTS, "\n")
defer delete(file_names)
file_count := len(file_names)
markdown_file_count: int
for line in file_names {
if strings.contains(line, ".md") {
markdown_file_count += 1
}
}
fmt.printf("There are %i files and %i of them are markdown files.\n", file_count, markdown_file_count)
// The fields proc will split the string with the separator being whitespace. Extra whitespace will be ignored.
command_string := "ls Downloads"
command_tokens := strings.fields(command_string)
defer delete(command_tokens)
fmt.println(command_tokens)
}