Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------------------------------------------------
- function string.split(s, pattern, maxsplit)
- if type(s) ~= "string" or type(pattern) ~= "string" or type(maxsplit) ~= "string" then
- return nil
- end
- if pattern == nil then
- pattern = " "
- end
- if maxsplit == nil then
- maxsplit = -1
- end
- if string.find(s, pattern) == nil then
- return {s}
- end
- local strings = {}
- local index = 1
- local last_index = 1
- while true do
- index = string.find(s, pattern, index)
- if index == nil or #strings == maxsplit then
- table.insert(strings, string.sub(s, last_index))
- return strings
- end
- table.insert(strings, string.sub(s, last_index, index - 1))
- index = index + #pattern
- last_index = index
- end
- end
Add Comment
Please, Sign In to add comment