Skip to content

String

The String module provides various extensions on the default Lua string library.

Separated Strings

One common pattern is to store lists of elements as a string with a fixed separator. The String module provides two APIs to help facilitate this as demonstrated below.

lua
local MyModule = select(2, ...).MyModule
local String = MyModule:From("LibTSMUtil"):Include("Lua.String")

local SEP = ","
local favoriteClasses = strjoin(SEP, "Rogue", "Mage", "Warrior")

print(String.SeparatedCount(favoriteClasses, SEP)) -- 3
print(String.SeparatedContains(favoriteClasses, SEP, "Rogue")) -- true
print(String.SeparatedContains(favoriteClasses, SEP, "Hunter")) -- false

for class in String.SplitIterator(favoriteClasses, SEP) do
   print(class)
end
-- Rogue
-- Mage
-- Warrior

API

Inherits: LibTSMModule

Functions

Escape

lua
function Lua.String.Escape(str)
  -> string

Escapes any magic characters used by lua's pattern matching.

Parameters:

NameTypeDescription
strstringThe string to be escaped

Returns:

TypeDescription
string

FormatToMatchPattern

lua
function Lua.String.FormatToMatchPattern(str)
  -> string

Generates a pattern matching string from a format string.

Parameters:

NameTypeDescription
strstringThe format string

Returns:

TypeDescription
string

SafeSplit

lua
function Lua.String.SafeSplit(str, sep, resultTbl)
  -> string[]

Splits a string in a way which won't cause stack overflows for large inputs. The lua strsplit function causes a stack overflow if passed large inputs. This API fixes that issue and also supports separators which are more than one character in length.

Parameters:

NameTypeDescription
strstringThe string to be split
sepstringThe separator to use to split the string
resultTbltableAn optional table to store the result in

Returns:

TypeDescription
string[]

SeparatedContains

lua
function Lua.String.SeparatedContains(str, sep, value)
  -> boolean

Check if a string which contains multiple values separated by a specific string contains the value.

Parameters:

NameTypeDescription
strstringThe string to be searched
sepstringThe separating string
valuestringThe value to search for

Returns:

TypeDescription
boolean

SeparatedCount

lua
function Lua.String.SeparatedCount(str, sep)
  -> number

Gets the count of separated string parts.

Parameters:

NameTypeDescription
strstringThe string to count the parts in
sepstringThe separating string

Returns:

TypeDescription
number

SplitIterator

lua
function Lua.String.SplitIterator(str, sep)
  -> fun():string

Iterates over the parts of a string which are separated by a character.

Parameters:

NameTypeDescription
strstringThe string to be split
sepstringThe separator to use to split the string

Returns:

TypeDescription
fun():stringIterator with fields: part

StrictMatch

lua
function Lua.String.StrictMatch(str, pattern, pos)
  -> ...string

Calls strmatch() and enforces that there's a match.

Parameters:

NameTypeDescription
strstring
patternstring
posnumber

Returns:

TypeDescription
...string

StrictMatchNumber

lua
function Lua.String.StrictMatchNumber(str, pattern, pos)
  -> number

Calls strmatch() and converts the result to a number, erroring if it's not a valid number.

Parameters:

NameTypeDescription
strstring
patternstring
posnumber

Returns:

TypeDescription
number