forked from Zinal001/awesome-sprak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
String Functions.txt
68 lines (61 loc) · 1.45 KB
/
String Functions.txt
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
# StrRepeat(str, times)
# Repeat a string x times
#
# Param {String} str - The string to repeat
# Param {Number} times - How many times it should repeat
#
# Author Zinal
# Return {String}
```python
String StrRepeat(str, times)
String newStr = ""
loop I from 1 to times
newStr += str
end
return newStr
end
```
# StrSub(str, start, length)
# Return a substring of a string
#
# Param {String} str - The original string
# Param {Number} start - The index to start at (Strings start at 0!)
# Param {Number} length - The amount of characters to extract
#
# Author Zinal
# Return {String}
String StrSub(str, start, length)
String newStr = ""
if(start < 0 || length <= 0)
return ""
end
loop I from start to start + length - 1
if (I >= Count(str))
break
end
newStr += str[I]
end
return newStr
end
# StrSplit(separator, str)
# Split a string into an array
#
# Param {String} separator - The separator that the string should be split with
# Param {String} str - The original string
#
# Depends on StrSub()
#
# Author Zinal
# Return {Array}
Array StrSplit(separator, str)
Array parts = []
number lastI = 0
loop I from 0 to Count(str) - 1
if (str[I] == separator)
Append(parts, StrSub(str, lastI, I - lastI))
lastI = I + 1
end
end
Append(parts, StrSub(str, lastI, Count(str) - 1 - lastI))
return parts
end