I need to do some string splitting for this text compression algorithm, but it must keep the separators.
Pretty simple, say I have a string:
eabdeeceda
I want to split the string by "e"
string.split(str, "e")
Problem is that it removes the separators.
abd, c, da
I want
e, abd, e, e, c, e, da
I've tried to think of various manual methods but they all seem to fall flat in the end.
Here's my best attempt
function splitKeep(str, separator)
local minimum = 0
local tbl = {}
repeat
local a, b = string.find(str, separator, minimum) -- find first separator
minimum = b -- set minimum
local inner
if string.sub(str, b, b+(b-a)) ~= separator then -- if the next section is not the same as the separator
local c = string.find(str, separator, minimum) -- find the next separator
inner = string.sub(str, b, c) -- get the string inbetween the end of the first separator and the start of the second
end
tbl[#tbl+1] = string.sub(str, b) -- insert the separator
tbl[#tbl+1] = inner -- insert the inside string
until string.find(str, separator, minimum) == nil
return tbl
end
I already know this wont work, it wont return anything before the first separator and after the last separator. It's also pretty messy.

1 comment
I like this question.