Skip to content

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

lua
string.startswith(str: <string>, prefix: <string>)

Parameter Description

ParameterDescription
strString to check
prefixPrefix string to match

Usage

lua
local s = "hello xmake"
if s:startswith("hello") then
    print("match")
end

See also string.endswith for checking string endings.

string.endswith

  • Determine if the end of the string matches

Function Prototype

API

lua
string.endswith(str: <string>, suffix: <string>)

Parameter Description

ParameterDescription
strString to check
suffixSuffix string to match

Usage

lua
local s = "hello xmake"
if s:endswith("xmake") then
    print("match")
end

string.split

  • Split string by separator

Function Prototype

API

lua
string.split(str: <string>, separator: <string>, options: <table>)

Parameter Description

ParameterDescription
strString to split
separatorSeparator string
optionsSplit options table (optional)

Usage

pattern match and ignore empty string

lua
("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

lua
("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

lua
("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

lua
("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

lua
("1\n\n2\n3"):split('\n', {limit = 2}) => 1, 2\n3
("1.2.3.4.5"):split('%.', {limit = 3}) => 1, 2, 3.4.5

string.trim

  • Remove the left and right whitespace characters of the string

Function Prototype

API

lua
string.trim(str: <string>)

Parameter Description

ParameterDescription
strString to trim

Usage

lua
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

lua
string.ltrim(str: <string>)

Parameter Description

ParameterDescription
strString to trim

Usage

lua
string.ltrim("    hello xmake!    ")

The result is: "hello xmake! "

string.rtrim

  • Remove the whitespace character to the right of the string

Function Prototype

API

lua
string.rtrim(str: <string>)

Parameter Description

ParameterDescription
strString to trim

Usage

lua
string.rtrim("    hello xmake!    ")

The result is: " hello xmake!"

string.lastof

  • Find the position of the last occurrence of a substring

Function Prototype

API

lua
string.lastof(str: <string>, pattern: <string>, plain?: <boolean>)

Parameter Description

ParameterDescription
strString to search
patternPattern to match
plainOptional. Whether to use plain text matching, default is false

Usage

lua
print(("src/test/file.lua"):lastof("/", true))  -- Output: 10
print(("abc.def.ghi"):lastof("%.", false))       -- Output: 8

string.replace

  • Replace text in a string

Function Prototype

API

lua
string.replace(str: <string>, old: <string>, new: <string>, opt?: <table>)

Parameter Description

ParameterDescription
strString to process
oldOld string to replace
newNew replacement string
optOptional. Options table, supports {plain = true} for plain text replacement

Usage

By default, uses Lua pattern matching for replacement:

lua
print(("hello world"):replace("world", "xmake"))
-- Output: hello xmake

Use plain text mode (avoids special characters being treated as patterns):

lua
print(("hello (world)"):replace("(world)", "xmake", {plain = true}))
-- Output: hello xmake

To replace content directly in a file, use io.replace or io.gsub.

string.serialize

  • Serialize an object to a string

Function Prototype

API

lua
string.serialize(object: <any>, opt?: <table>)

Parameter Description

ParameterDescription
objectObject to serialize
optOptional. Options table, supports {strip = true, binary = false, indent = true}

Usage

lua
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

lua
string.deserialize(str: <string>)

Parameter Description

ParameterDescription
strString to deserialize

Usage

lua
local obj = ("{a = 1, b = 'hello'}"):deserialize()
print(obj.a)  -- Output: 1
print(obj.b)  -- Output: hello

The 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

lua
string.ipattern(pattern: <string>, brackets?: <boolean>)

Parameter Description

ParameterDescription
patternLua pattern string
bracketsOptional. Whether to also convert letters inside brackets [], default is false

Usage

lua
print(string.ipattern("src/.*%.c"))
-- Output: [sS][rR][cC]/.*%.[cC]

print(("SRC/test.C"):match(string.ipattern("src/.*%.c")))
-- Output: SRC/test.C

string.levenshtein

  • Compute the Levenshtein (edit) distance between two strings

Function Prototype

API

lua
string.levenshtein(str1: <string>, str2: <string>, opt?: <table>)

Parameter Description

ParameterDescription
str1First string
str2Second string
optOptional. Options table, supports {sub = 1, ins = 1, del = 1} for substitution/insertion/deletion costs

Usage

lua
print(("hello"):levenshtein("hallo"))  -- Output: 1
print(("kitten"):levenshtein("sitting"))  -- Output: 3