string.gsub returns both the replaced string and the number of replacements.

Replace Text

replace_text.lua
local text = 
local replaced, count = string.gsub(text, ",", "|")

print("text=" .. text)
print("replaced=" .. replaced)
print("count=" .. count)
local text = 
local replaced, count = string.gsub(text, ",", "|")

print("text=" .. text)
print("replaced=" .. replaced)
print("count=" .. count)
local text = 
local replaced, count = string.gsub(text, ",", "|")

print("text=" .. text)
print("replaced=" .. replaced)
print("count=" .. count)
replacement count The second return value from `gsub` is useful when later logic needs to know whether anything changed.