Module:StringReplace: Difference between revisions
From Terra Invicta Official Wiki
Created page with "-- This is a helper module for replacing text in strings local p = {} local sfind = string.find local ssub = string.sub -- Replaces ONE instance of the stringToReplace with replacementString in the fullString function replace(fullString, stringToReplace, replacementString) local si, ei = sfind(fullString, stringToReplace, 1, true) if not si then return fullString end local prefix = ssub(fullString,1,si - 1) local suffix = ssub(fullString,ei+1) return prefix ....." |
No edit summary |
||
| Line 7: | Line 7: | ||
-- Replaces ONE instance of the stringToReplace with replacementString in the fullString | -- Replaces ONE instance of the stringToReplace with replacementString in the fullString | ||
function replace(fullString, stringToReplace, replacementString) | function p.replace(fullString, stringToReplace, replacementString) | ||
local si, ei = sfind(fullString, stringToReplace, 1, true) | local si, ei = sfind(fullString, stringToReplace, 1, true) | ||
if not si then | if not si then | ||
Latest revision as of 18:58, 19 January 2026
Documentation for this module may be created at Module:StringReplace/doc
-- This is a helper module for replacing text in strings
local p = {}
local sfind = string.find
local ssub = string.sub
-- Replaces ONE instance of the stringToReplace with replacementString in the fullString
function p.replace(fullString, stringToReplace, replacementString)
local si, ei = sfind(fullString, stringToReplace, 1, true)
if not si then
return fullString
end
local prefix = ssub(fullString,1,si - 1)
local suffix = ssub(fullString,ei+1)
return prefix .. replacementString .. suffix
end
return p


