trim

function trim( str ) --> nstr

Description

Returns the string stripped of whitespace from both ends.

Parameters

str

The string that should be trimmed.

Return Values

nstr

A trimmed version of the assigned parameter string.

Code

--ZFUNC-trim-v1
local function trim( str ) --> nstr
   local n = str:find( "%S" )
   return n and str:match( ".*%S", n ) or ""
end

return trim

Examples

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

t( trim( "" ), "" )
t( trim( " " ), "" )
t( trim( "  " ), "" )
t( trim( "a" ), "a" )
t( trim( " a" ), "a" )
t( trim( "a " ), "a" )
t( trim( " a " ), "a" )
t( trim( "  a  " ), "a" )
t( trim( " ab cd " ), "ab cd" )
t( trim( " \t\r\n\f\va\000b \r\t\n\f\v" ), "a\000b" )

t()