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( "tapered" )
local chomp = require( "chomp" )

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

t.done()

Inspired by

See also