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:
::: tip API
string.startswith(str: <string>, prefix: <string>)
:::
| Parameter | Description |
|---|---|
| str | String to check |
| prefix | Prefix string to match |
local s = "hello xmake"
if s:startswith("hello") then
print("match")
end
::: tip API
string.endswith(str: <string>, suffix: <string>)
:::
| Parameter | Description |
|---|---|
| str | String to check |
| suffix | Suffix string to match |
local s = "hello xmake"
if s:endswith("xmake") then
print("match")
end
::: tip API
string.split(str: <string>, separator: <string>, options: <table>)
:::
| Parameter | Description |
|---|---|
| str | String to split |
| separator | Separator string |
| options | Split options table (optional) |
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, abc
plain match and ignore empty string
("1\n\n2\n3"):split('\n', {plain = true}) => 1, 2, 3
("abc123123xyz123abc"):split('123', {plain = true}) => abc, xyz, abc
pattern 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, abc
plain 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, abc
limit 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.5
::: tip API
string.trim(str: <string>)
:::
| Parameter | Description |
|---|---|
| str | String to trim |
string.trim(" hello xmake! ")
The result is: "hello xmake!"
::: tip API
string.ltrim(str: <string>)
:::
| Parameter | Description |
|---|---|
| str | String to trim |
string.ltrim(" hello xmake! ")
The result is: "hello xmake! "
::: tip API
string.rtrim(str: <string>)
:::
| Parameter | Description |
|---|---|
| str | String to trim |
string.rtrim(" hello xmake! ")
The result is: " hello xmake!"