Vital.sandbox
String

string.sub

Shared

Extracts a substring from a string


Syntax

local result = string.sub(input, start_at, end_at = -1)

Parameters

TypeNameDescription
stringinputString to be extract from
intstart_atCharacter position where extraction begins - negative counts from end
intend_atCharacter position where extraction ends - negative counts from end

Returns

TypeNameDescription
stringresultExtracted substring within string's specified range

Examples

--Extract substring
local result = string.sub("hello world", 1, 5)
engine.print(result) --hello
--From position to end
local result = string.sub("hello world", 7)
engine.print(result) --world
--Get last characters using negative index
local result = string.sub("hello", -3)
engine.print(result) --llo
--Using negative end position
local result = string.sub("hello world", 1, -7)
engine.print(result) --hello
--Get middle portion
local result = string.sub("hello world", 2, -2)
engine.print(result) --ello worl
--Single character
local char = string.sub("hello", 1, 1)
engine.print(char) --h
--Last character
local last = string.sub("hello", -1)
engine.print(last) --o
--Extract file extension
local filename = "document.txt"
local ext = string.sub(filename, -3)
engine.print(ext) --txt
--Get first word
local text = "hello world"
local space = string.find(text, " ")
local firstWord = string.sub(text, 1, space - 1)
engine.print(firstWord) --hello
--Remove first and last character
local str = "[hello]"
local inner = string.sub(str, 2, -2)
engine.print(inner) --hello
--Equivalent to entire string
local str = "hello"
local copy = string.sub(str, 1, -1)
engine.print(copy) --hello
--Empty substring (start_at > end_at)
local result = string.sub("hello", 5, 3)
engine.print(result) --
--Beyond string length
local result = string.sub("hello", 1, 100)
engine.print(result) --hello
--Negative starting position
local result = string.sub("hello", -5, -1)
engine.print(result) --hello
--Extract year from date
local date = "2024-01-15"
local year = string.sub(date, 1, 4)
engine.print(year) --2024
--Extract month
local date = "2024-01-15"
local month = string.sub(date, 6, 7)
engine.print(month) --01
--Split string into chunks
local str = "abcdefgh"
for i = 1, #str, 2 do
    local chunk = string.sub(str, i, i + 1)
    engine.print(chunk)
end
--Output: ab, cd, ef, gh
--Truncate string
local long = "This is a very long string"
local short = string.sub(long, 1, 10).."..."
engine.print(short) --This is a ...
--Compare with sub-range
local str = "hello world"
if string.sub(str, 1, 5) == "hello" then
    engine.print("Starts with hello")
end
--Extract between indices
local str = "prefix_content_suffix"
local content = string.sub(str, 8, 14)
engine.print(content) --content

On this page