0Pricing
Lua Academy · Lesson

string.gsub for Transformations

Replace patterns with strings, functions, or table lookups via gsub.

What Is string.gsub?

string.gsub(s, pattern, repl, n) replaces occurrences of pattern in s with repl. Returns the new string and the number of substitutions.

Simple String Replacement

Replace all occurrences of a literal substring.

local s = "hello world world"
local result, count = s:gsub("world", "Lua")
print(result, count)  -- hello Lua Lua  2

All lessons in this course

  1. Lua Pattern Syntax
  2. Captures and string.match
  3. string.gmatch for Tokenization
  4. string.gsub for Transformations
← Back to Lua Academy