String Extensions
Side: Shared (Client + Server)
Extensions to Lua's built-in string library. Instance methods use the : syntax, static methods use . syntax.
Instance Methods
string:firstToUpper()
Capitalizes the first character of the string.
Returns: string
string:split(delimiter, max_parts?)
Splits a string by the given delimiter.
| Parameter | Type | Default | Description |
|---|---|---|---|
delimiter | string | — | Separator pattern |
max_parts | integer? | nil | Maximum number of parts (unlimited if omitted) |
Returns: table (array of strings)
string:trim()
Removes leading and trailing whitespace.
Returns: string
string:toHex()
Converts a hex string to a number.
Returns: number
string:removeAccent()
Replaces accented characters with their ASCII equivalents.
Returns: string
string:convertVersion()
Converts a version string (e.g. "1.2.3") to a comparable number.
Returns: number
string:compareVersionWith(other)
Compares two version strings.
| Parameter | Type | Description |
|---|---|---|
other | string | Version string to compare against |
Returns: integer — -1 if self is older, 0 if equal, 1 if self is newer.
Static Methods
string.spaceNumber(value, decimal_count?)
Formats a number with space-separated thousands.
| Parameter | Type | Default | Description |
|---|---|---|---|
value | number | — | Number to format |
decimal_count | integer? | nil | Decimal places to show |
Returns: string
string.compare(first, second, case_sensitive?)
Natural sort comparison of two strings (numbers within strings are compared numerically).
| Parameter | Type | Default | Description |
|---|---|---|---|
first | string | — | First string |
second | string | — | Second string |
case_sensitive | boolean? | false | Case-sensitive comparison |
Returns: integer — -1, 0, or 1
Examples
-- Capitalize
("hello"):firstToUpper() -- "Hello"
-- Split
("a,b,c"):split(",") -- { "a", "b", "c" }
("a,b,c,d"):split(",", 2) -- { "a", "b,c,d" }
-- Trim
(" hello "):trim() -- "hello"
-- Remove accents
("cafe"):removeAccent() -- "cafe"
-- Format numbers with spaces
string.spaceNumber(1234567) -- "1 234 567"
string.spaceNumber(1234567.89, 2) -- "1 234 567.89"
-- Version comparison
local v = "1.2.0"
v:compareVersionWith("1.1.0") -- 1 (newer)
v:compareVersionWith("1.2.0") -- 0 (equal)
v:compareVersionWith("1.3.0") -- -1 (older)
-- Natural sort
string.compare("item2", "item10") -- -1 (item2 comes before item10)
