util.string.insert

Shared

Inserts a substring into a string at a given character position


Syntax

local result = util.string.insert(
    input,
    position = nil,
    substring
)

Parameters

TypeNameDescription
stringinputString to insert into
intpositionCharacter position to insert at - negative counts from end - when omitted, substring is appended
stringsubstringString to insert

Returns

TypeNameDescription
stringresultString with substring inserted

Examples

Append to the end of a string
local result = util.string.insert("hello", "!")

core.engine.print("info", result) -- 'hello!'
Insert at a specific character position
local result = util.string.insert("hello", 1, "X")

core.engine.print("info", result) -- 'Xhello'
Insert in the middle of a string
local result = util.string.insert("hllo", 2, "e")

core.engine.print("info", result) -- 'hello'
Use a negative position to insert from the end
local result = util.string.insert("hello", -1, "p")

core.engine.print("info", result) -- 'hellpo'

On this page