Lua – code to split string into lines based on carriage return and newlines


function SplitMeIntolines(str)
local t = {}
local function helper(line) table.insert(t, line) return "" end
helper((str:gsub("(.-)r?n", helper)))
return t
end

In

5 responses

  1. It is missing some backslashes, right?
    function SplitMeIntolines(str)
    local t = {}
    local function helper(line) table.insert(t, line) return “” end
    helper((str:gsub(“(.-)\r?\n”, helper)))
    return t
    end

    1. kmdarshan Avatar
      kmdarshan

      Thanks for bringing this up.

  2. No problem. Seems to work fine now: http://tinybrain.de/1004715
    You doing Lua these days? 🙂
    All the best,
    Stefan

  3. kmdarshan Avatar
    kmdarshan

    Yeah, I try doing a little bit of everything. Whatever is the need of the hour 🙂

  4. How about a warning.. this is a recursive solution. It will run out of stackmemory for real huge documents. In addition it only works for windows lineendings-

Leave a Reply

Your email address will not be published. Required fields are marked *