string.lua

main 60 lines · 1.3 KB Raw
Alex Alex Commit Client Upload Part 2 19/10/2026 00:06
1-- @docclass string
2
3function string:split(delim)
4 local start = 1
5 local results = {}
6 while true do
7 local pos = string.find(self, delim, start, true)
8 if not pos then
9 break
10 end
11 table.insert(results, string.sub(self, start, pos-1))
12 start = pos + string.len(delim)
13 end
14 table.insert(results, string.sub(self, start))
15 table.removevalue(results, '')
16 return results
17end
18
19function string:starts(start)
20 return string.sub(self, 1, #start) == start
21end
22
23function string:ends(test)
24 return test =='' or string.sub(self,-string.len(test)) == test
25end
26
27function string:trim()
28 return string.match(self, '^%s*(.*%S)') or ''
29end
30
31function string:explode(sep, limit)
32 if type(sep) ~= 'string' or tostring(self):len() == 0 or sep:len() == 0 then
33 return {}
34 end
35
36 local i, pos, tmp, t = 0, 1, "", {}
37 for s, e in function() return string.find(self, sep, pos) end do
38 tmp = self:sub(pos, s - 1):trim()
39 table.insert(t, tmp)
40 pos = e + 1
41
42 i = i + 1
43 if limit ~= nil and i == limit then
44 break
45 end
46 end
47
48 tmp = self:sub(pos):trim()
49 table.insert(t, tmp)
50 return t
51end
52
53function string:contains(str, checkCase, start, plain)
54 if(not checkCase) then
55 self = self:lower()
56 str = str:lower()
57 end
58 return string.find(self, str, start and start or 1, plain == nil and true or false)
59end
60
Top