string
The string module is a native module of lua. For details, see: lua official manual
It has been extended in xmake to add some extension interfaces:
For table operations, see the table module. For file I/O, see the io module.
string.startswith
- Determine if the beginning of the string matches
Function Prototype
API
string.startswith(str: <string>, prefix: <string>)Parameter Description
| Parameter | Description |
|---|---|
| str | String to check |
| prefix | Prefix string to match |
Usage
local s = "hello xmake"
if s:startswith("hello") then
print("match")
endSee also string.endswith for checking string endings.
string.endswith
- Determine if the end of the string matches
Function Prototype
API
string.endswith(str: <string>, suffix: <string>)Parameter Description
| Parameter | Description |
|---|---|
| str | String to check |
| suffix | Suffix string to match |
Usage
local s = "hello xmake"
if s:endswith("xmake") then
print("match")
endstring.split
- Split string by separator
Function Prototype
API
string.split(str: <string>, separator: <string>, options: <table>)Parameter Description
| Parameter | Description |
|---|---|
| str | String to split |
| separator | Separator string |
| options | Split options table (optional) |
Usage
pattern match and ignore empty string
("1\n\n2\n3"):split('\n') => 1, 2, 3
("abc123123xyz123abc"):split('123') => abc, xyz, abc
("abc123123xyz123abc"):split('[123]+') => abc, xyz, abcplain match and ignore empty string
("1\n\n2\n3"):split('\n', {plain = true}) => 1, 2, 3
("abc123123xyz123abc"):split('123', {plain = true}) => abc, xyz, abcpattern match and contains empty string
("1\n\n2\n3"):split('\n', {strict = true}) => 1, , 2, 3
("abc123123xyz123abc"):split('123', {strict = true}) => abc, , xyz, abc
("abc123123xyz123abc"):split('[123]+', {strict = true}) => abc, xyz, abcplain match and contains empty string
("1\n\n2\n3"):split('\n', {plain = true, strict = true}) => 1, , 2, 3
("abc123123xyz123abc"):split('123', {plain = true, strict = true}) => abc, , xyz, abclimit split count
("1\n\n2\n3"):split('\n', {limit = 2}) => 1, 2\n3
("1.2.3.4.5"):split('%.', {limit = 3}) => 1, 2, 3.4.5string.trim
- Remove the left and right whitespace characters of the string
Function Prototype
API
string.trim(str: <string>)Parameter Description
| Parameter | Description |
|---|---|
| str | String to trim |
Usage
string.trim(" hello xmake! ")The result is: "hello xmake!"
To trim only the left side, use string.ltrim; for the right side only, use string.rtrim.
string.ltrim
- Remove the whitespace character to the left of the string
Function Prototype
API
string.ltrim(str: <string>)Parameter Description
| Parameter | Description |
|---|---|
| str | String to trim |
Usage
string.ltrim(" hello xmake! ")The result is: "hello xmake! "
string.rtrim
- Remove the whitespace character to the right of the string
Function Prototype
API
string.rtrim(str: <string>)Parameter Description
| Parameter | Description |
|---|---|
| str | String to trim |
Usage
string.rtrim(" hello xmake! ")The result is: " hello xmake!"
string.lastof
- Find the position of the last occurrence of a substring
Function Prototype
API
string.lastof(str: <string>, pattern: <string>, plain?: <boolean>)Parameter Description
| Parameter | Description |
|---|---|
| str | String to search |
| pattern | Pattern to match |
| plain | Optional. Whether to use plain text matching, default is false |
Usage
print(("src/test/file.lua"):lastof("/", true)) -- Output: 10
print(("abc.def.ghi"):lastof("%.", false)) -- Output: 8string.replace
- Replace text in a string
Function Prototype
API
string.replace(str: <string>, old: <string>, new: <string>, opt?: <table>)Parameter Description
| Parameter | Description |
|---|---|
| str | String to process |
| old | Old string to replace |
| new | New replacement string |
| opt | Optional. Options table, supports {plain = true} for plain text replacement |
Usage
By default, uses Lua pattern matching for replacement:
print(("hello world"):replace("world", "xmake"))
-- Output: hello xmakeUse plain text mode (avoids special characters being treated as patterns):
print(("hello (world)"):replace("(world)", "xmake", {plain = true}))
-- Output: hello xmakeTo replace content directly in a file, use io.replace or io.gsub.
string.serialize
- Serialize an object to a string
Function Prototype
API
string.serialize(object: <any>, opt?: <table>)Parameter Description
| Parameter | Description |
|---|---|
| object | Object to serialize |
| opt | Optional. Options table, supports {strip = true, binary = false, indent = true} |
Usage
local str = string.serialize({a = 1, b = "hello"})
print(str)The reverse operation is string.deserialize, which restores a serialized string back to an object. To serialize and save an object to a file, use io.save.
string.deserialize
- Deserialize a string to an object
Function Prototype
API
string.deserialize(str: <string>)Parameter Description
| Parameter | Description |
|---|---|
| str | String to deserialize |
Usage
local obj = ("{a = 1, b = 'hello'}"):deserialize()
print(obj.a) -- Output: 1
print(obj.b) -- Output: helloThe reverse operation is string.serialize. To load serialized data from a file, use io.load.
string.ipattern
- Generate a case-insensitive matching pattern
Function Prototype
API
string.ipattern(pattern: <string>, brackets?: <boolean>)Parameter Description
| Parameter | Description |
|---|---|
| pattern | Lua pattern string |
| brackets | Optional. Whether to also convert letters inside brackets [], default is false |
Usage
print(string.ipattern("src/.*%.c"))
-- Output: [sS][rR][cC]/.*%.[cC]
print(("SRC/test.C"):match(string.ipattern("src/.*%.c")))
-- Output: SRC/test.Cstring.levenshtein
- Compute the Levenshtein (edit) distance between two strings
Function Prototype
API
string.levenshtein(str1: <string>, str2: <string>, opt?: <table>)Parameter Description
| Parameter | Description |
|---|---|
| str1 | First string |
| str2 | Second string |
| opt | Optional. Options table, supports {sub = 1, ins = 1, del = 1} for substitution/insertion/deletion costs |
Usage
print(("hello"):levenshtein("hallo")) -- Output: 1
print(("kitten"):levenshtein("sitting")) -- Output: 3