tm.string
String helpers. Shared.
tm.string is the string table of the calling resource, extended with the functions below, and only once this import has run (imports load lazily per resource). Always call it through tm.string (tm.string.trim(value)), or preload it with a tm 'string' line in the manifest before using the method form (value:trim()).
lua
tm.string.trim(value) -- ' hi ' -> 'hi', nil -> ''
tm.string.trimOrNil(value) -- blank or non-string -> nil, for NUI inputs
tm.string.commas(1234567.4) -- '1,234,567'
tm.string.money(1234.5) -- '$1,235'
tm.string.money(1234.5, 2) -- '$1,234.50'
tm.string.money(-12) -- '-$12'
tm.string.appendText('hello', 'world') -- 'hello world'
tm.string.appendLine('ab', 'X', 5) -- 'ab X'| Function | Notes |
|---|---|
trim(value) | Strips leading and trailing whitespace. nil becomes '', other types are tostring'd. |
trimOrNil(value) | Trims a string and returns nil when nothing is left. Non-strings return nil. |
commas(value) | Whole number with thousands separators. Halves round up. |
money(value, decimals?) | A dollar amount with thousands separators. Halves round up. Negatives read -$12. |
appendText(text, append) | Joins with one space. An empty text gets no leading space. Returns nil for a nil text and errors for a nil append. |
appendLine(text, append, width) | Pads text with spaces up to width characters, then appends. Text already width or longer is not padded. |
Anything that is not a finite number (nil, 'abc', NaN, infinity) counts as 0 in commas and money. A value that rounds to zero never gets a minus sign.
appendLine pads to exactly width.
For a plain deep copy use table.deepclone (ox_lib). It does not handle cycles, so keep a local copy only where a depth limit or cycle guard is the point.