Definition: |
object res = trim(object source, object what=" \t\r\n", integer ret_index=0)
or ... object res = trim_head(object source, object what=" \t\r\n", integer ret_index=0) -- or -- object res = trim_tail(object source, object what=" \t\r\n", integer ret_index=0) |
Description: |
Trim whitespace from strings.
source: object (string) to be trimmed. what: elements (characters) to be removed. ret_index: if non-zero, return {startpos,endpos}, startpos, or endpos respectively. |
Comments: |
Can theoretically be used for other purposes besides trimming whitespace, perhaps to trim unused entries from a table.
When ret_index is non-zero, the result is {startpos,endpos} (trim), startpos (trim_head), or endpos (trim_tail). To simplify processing, if source is not a sequence/string, but instead something like EOF (ie -1), it is returned unaltered. Note however that feature is certainly not intended for use when working with indexes (ret_index!=0), since treating a -1 (or whatever) result as an index would probably be quite wrong! Instead it is usually easier to test whether the source is a sequence and avoid the call, rather than struggle to interpret the result correctly (that is, when ret_index is non-zero). |
Example: |
?trim(" abc ") -- prints "abc" ?trim("\"abc\"") -- prints ""abc"" ?trim("\"abc\"",'\"') -- prints "abc" (not ""abc"") ?trim(table,{{}}) -- removes s[i]={} entries from start and end (note the extra {}) ?trim(table,{{},0}) -- removes s[i]={} or 0 "" ?trim(gets(fn)) -- trim returns eof (-1) unaltered. |