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.
I would use brackets in this case, I don't know if split will work here but string.gsub is what you would use, you can Google String Formatting page, it says everything about it. You would put ,
after each e
and the you would do string.split(str, ',')
. I'm on phone so sorry about little no details but here is example:
local str = 'eabdeeceda' str = string.gsub(str, '(e)', '%1,') print(string.split(str, ','))
where bracket in which e is, it let's you access third arguent, %1 refers to match that has been found and is inside the bracket so %1 in this case refers to e as its the only letter in the bracket.