File tree Expand file tree Collapse file tree 2 files changed +19
-0
lines changed Expand file tree Collapse file tree 2 files changed +19
-0
lines changed Original file line number Diff line number Diff line change 97
97
# @author https://github.com/SuperFola
98
98
(let setAt (fun (_str _index _x) (builtin__string:setAt _str _index _x)))
99
99
100
+ # @brief Count the number of non-overlapping occurrences of a word in a string
101
+ # @param _str string to search into
102
+ # @param _word word to count occurrences of
103
+ # =begin
104
+ # (string:count "the three truths" "th") # 3
105
+ # =end
106
+ # @author https://github.com/SuperFola
107
+ (let count (fun (_str _word) {
108
+ (mut _count 0)
109
+ (mut _next (find _str _word))
110
+ (while (!= -1 _next) {
111
+ (set _count (+ 1 _count))
112
+ (set _next (findAfter _str _word (+ _next (len _word)))) })
113
+ _count }))
114
+
100
115
# @brief Converts the given character to lowercase.
101
116
# @param _string the string to make lowercase
102
117
# @details The original string is left unmodified.
Original file line number Diff line number Diff line change 12
12
(test:eq (builtin__string:ord "a") (string:ord "a"))
13
13
(test:eq (builtin__string:chr 65) (string:chr 65))
14
14
15
+ (test:eq 3 (string:count "the three truths" "th"))
16
+ (test:eq 0 (string:count "the three truths" "thz"))
17
+ (test:eq 2 (string:count "ababababab" "abab"))
18
+
15
19
(test:eq "abcdefghijklmnopqrstuvwxyz" (string:toLower "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
16
20
(test:eq "abcdefghijklmnopqrstuvwxyz" (string:toLower "abcdefghijklmnopqrstuvwxyz"))
17
21
(test:eq "ABCDEFGHIJKLMNOPQRSTUVWXYZ" (string:toUpper "abcdefghijklmnopqrstuvwxyz"))
You can’t perform that action at this time.
0 commit comments