Trims leading and trailing characters (whitespace by default) from a string. Also available as string.trim (though not provided by the Lua standard library).

See also Utility functions.


text = strtrim("str" [, "trimChars"])


  • str - A string to trim (string)
  • trimChars - A string listing the characters to be trimmed (e.g. "[]{}()" to trim leading and trailing brackets, braces, and parentheses); if nil or omitted, whitespace characters (space, tab, newline, etc) are trimmed (string)


  • text - The trimmed string (string)


strtrim("   This is a test   ")
-- Returns "This is a test"
strtrim("121abc456", "615")
-- Returns "21abc4"