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 a substring by start and end position
local result = string.sub("hello world", 1, 5)

engine.print("info", result) --'hello'
Extract from a position to the end
local result = string.sub("hello world", 7)

engine.print("info", result) --'world'
Get the last characters using a negative index
local result = string.sub("hello", -3)

engine.print("info", result) --'llo'
Use a negative end position
local result = string.sub("hello world", 1, -7)

engine.print("info", result) --'hello'
Extract the middle portion of a string
local result = string.sub("hello world", 2, -2)

engine.print("info", result) --'ello worl'
Extract a single character
local char = string.sub("hello", 1, 1)

engine.print("info", char) --'h'
Extract the last character
local last = string.sub("hello", -1)

engine.print("info", last) --'o'
Extract a file extension
local filename = "document.txt"
local ext = string.sub(filename, -3)

engine.print("info", ext) --'txt'
Extract the first word from a string
local text = "hello world"
local space = string.find(text, " ")
local firstWord = string.sub(text, 1, space - 1)

engine.print("info", firstWord) --'hello'
Remove the first and last character
local str = "[hello]"
local inner = string.sub(str, 2, -2)

engine.print("info", inner) --'hello'
Sub with full range returns the entire string
local str = "hello"
local copy = string.sub(str, 1, -1)

engine.print("info", copy) --'hello'
Return empty string when start exceeds end
local result = string.sub("hello", 5, 3)

engine.print("info", result) --''
Clamped to string length when end exceeds bounds
local result = string.sub("hello", 1, 100)

engine.print("info", result) --'hello'
Use a negative start position
local result = string.sub("hello", -5, -1)

engine.print("info", result) --'hello'
Extract the year from a date string
local date = "2024-01-15"
local year = string.sub(date, 1, 4)

engine.print("info", year) --'2024'
Extract the month from a date string
local date = "2024-01-15"
local month = string.sub(date, 6, 7)

engine.print("info", month) --'01'
Split a string into fixed-size chunks
local str = "abcdefgh"
for i = 1, #str, 2 do
    local chunk = string.sub(str, i, i + 1)
    engine.print("info", chunk)
end

--[[
Output: 
'ab'
'cd'
'ef'
'gh'
]]
Truncate a long string with ellipsis
local long = "This is a very long string"
local short = string.sub(long, 1, 10).."..."

engine.print("info", short) --'This is a ...'
Check if a string starts with a prefix
local str = "hello world"

if string.sub(str, 1, 5) == "hello" then
    engine.print("info", "Starts with hello")
end
Extract content between known indices
local str = "prefix_content_suffix"
local content = string.sub(str, 8, 14)

engine.print("info", content) --'content'

On this page