How to remove last line from a string in Lua?

Viewed 1577

I am using Lua in World of Warcraft.

I have this string:

"This\nis\nmy\nlife."

So when printed, the output is this:

This
is
my
life.

How can I store the entire string except the last line in a new variable?

So I want the output of the new variable to be this:

This
is
my

I want the Lua code to find the last line (regardless of how many lines in the string), remove the last line and store the remaining lines in a new variable.

Thank you.

4 Answers

Most efficient solution is plain string.find.

local s = "This\nis\nmy\nlife." -- string with newlines
local s1 = "Thisismylife." -- string without newlines

local function RemoveLastLine(str)
    local pos = 0 -- start position
    while true do -- loop for searching newlines
        local nl = string.find(str, "\n", pos, true) -- find next newline, true indicates we use plain search, this speeds up on LuaJIT.
        if not nl then break end -- We didn't find any newline or no newlines left.
        pos = nl + 1 -- Save newline position, + 1 is necessary to avoid infinite loop of scanning the same newline, so we search for newlines __after__ this character
    end
    if pos == 0 then return str end -- If didn't find any newline, return original string

    return string.sub(str, 1, pos - 2) -- Return substring from the beginning of the string up to last newline (- 2 returns new string without the last newline itself
end

print(RemoveLastLine(s))
print(RemoveLastLine(s1))

Keep in mind this works only for strings with \n-style newlines, if you have \n\r or \r\n easier solution would be a pattern.

This solution is efficient for LuaJIT and for long strings. For small strings string.sub(s1, 1, string.find(s1,"\n[^\n]*$") - 1) is fine (Not on LuaJIT tho).

So I found that Egor Skriptunoff's solutions in the comments worked very well indeed but I am unable to mark his comments as an answer so I'll put his answers here.

This removes the last line and stores the remaining lines in a new variable:

new_str = old_str:gsub("\n[^\n]*$", "")

If there is a new line marker at the end of the last line, Egor posted this as a solution:

new_str = old_str:gsub("\n[^\n]*(\n?)$", "%1")

While this removes the first line and stores the remaining lines in a new variable:

first_line = old_str:match("[^\n]*")

Thanks for your help, Egor.

I scan it backward because it more easier to remove thing from back with backward scanning rather than forward it would be more complex if you scan forward and much simpler scanning backward

I succeed it in one take

function removeLastLine(str) --It will return empty string when there just 1 line
  local letters = {}
  for let in string.gmatch(str, ".") do --Extract letter by letter to a table
    table.insert(letters, let)
  end

  local i = #letters --We're scanning backward
  while i >= 0 do --Scan from bacward
    if letters[i] == "\n" then
      letters[i] = nil
      break
    end
    letters[i] = nil --Remove letter from letters table
    i = i - 1
  end
  return table.concat(letters)
end

print("This\nis\nmy\nlife.")
print(removeLastLine("This\nis\nmy\nlife."))

How the code work

  1. The letters in str argument will be extracted to a table ("Hello" will become {"H", "e", "l", "l", "o"})

  2. i local is set to the end of the table because we scan it from the back to front

  3. Check if letters[i] is \n if it newline then goto step 7

  4. Remove entry at letters[i]

  5. Minus i with 1

  6. Goto step 3 until i is zero if i is zero then goto step 8

  7. Remove entry at letters[i] because it havent removed when checking for newline

  8. Return table.concat(letters). Won't cause error because table.concat return empty string if the table is empty

#! /usr/bin/env lua

local serif = "Is this the\nreal life?\nIs this\njust fantasy?"
local reversed = serif :reverse()  --  flip it

local pos = reversed :find( '\n' ) +1  --  count backwards
local sans_serif = serif :sub( 1, -pos )  --  strip it

print( sans_serif )

you can oneline it if you want, same results.

local str = "Is this the\nreal life?\nIs this\njust fantasy?"
print(  str :sub( 1,  -str :reverse() :find( '\n' ) -1 )  )

Is this the
real life?
Is this

Related