Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function string:contains(sub)
- return self:find(sub, 1, true) ~= nil
- end
- function string:startswith(start)
- return self:sub(1, #start) == start
- end
- function string:endswith(ending)
- return ending == "" or self:sub(-#ending) == ending
- end
- function string:replace(old, new)
- local s = self
- local searchStartIdx = 1
- while true do
- local startIdx, endIdx = s:find(old, searchStartIdx, true)
- if (not startIdx) then
- break
- end
- local postfix = s:sub(endIdx + 1)
- s = s:sub(1, (startIdx - 1)) .. new .. postfix
- searchStartIdx = -1 * postfix:len()
- end
- return s
- end
- function string:insert(pos, text)
- return self:sub(1, pos - 1) .. text .. self:sub(pos)
- end
- local function lstrip_whitespace(s)
- local _, pos = s:find("^%s+")
- if pos then
- s = s:sub(pos + 1)
- end
- return s
- end
- local function rstrip_whitespace(s)
- pos = s:find("%s+$")
- if pos then
- s = s:sub(1, pos - 1)
- end
- return s
- end
- function string:strip()
- local s = self
- if s == "" then
- return ""
- end
- s = lstrip_whitespace(s)
- s = rstrip_whitespace(s)
- return s
- end
- function string:strip_prefix(prefix)
- if prefix == nil then
- return lstrip_whitespace(self)
- end
- local s = self
- local len = #s
- local pLen = #prefix
- if len == 0 or pLen == 0 or len < pLen then
- return s
- elseif s == prefix then
- return ""
- elseif s:sub(1, pLen) == prefix then
- return s:sub(pLen + 1)
- end
- return s
- end
- function string:strip_suffix(suffix)
- if suffix == nil then
- return rstrip_whitespace(self)
- end
- local s = self
- local len = #s
- local sLen = #suffix
- if len == 0 or sLen == 0 or len < sLen then
- return s
- elseif s == suffix then
- return ""
- elseif s:sub(-sLen) == suffix then
- return s:sub(1, len - sLen)
- end
- return s
- end
Advertisement
Add Comment
Please, Sign In to add comment