chomp

function chomp( str ) --> nstr

Description

Removes the endline separators from the end of str (if present). Endline separator means \n, \r or \r\n, whitespaces and tabs will not be removed.

Parameters

str

The string where the endline should be removed.

Return Values

nstr

String that has not a endline separator at the end.

Code

--ZFUNC-chomp-v1
local function chomp( str ) --> nstr
   return string.gsub( str, "[\n\r]*$", "" )
end

return chomp

Examples

local t = require( "taptest" )
local chomp = require( "chomp" )

t( chomp( "hello" ), "hello" )
t( chomp( "hello\n" ), "hello" )
t( chomp( "hello\r\n" ), "hello" )
t( chomp( "hello\n\r" ), "hello" )
t( chomp( "hello\r" ), "hello" )
t( chomp( "hello \n there" ), "hello \n there" )
t( chomp( "hello\r\n\r\n" ), "hello" )

t()

Inspired by

See also