Hi folks.
In my game I need to replace a single letter in a string with another letter.
I have code to do this, but I’d like something a little more elegant and hopefully faster.
( Motivation: When I do this operation, I may do it hundreds of times in a row.)
local function replaceAt( str, at, with ) if( at \< 1 or at \> string.len( str ) ) then return str elseif( at == 1 ) then return with .. strSub( str, at+1, string.len(str) ) elseif( at == string.len( str ) ) then return strSub( str, 1, at-1 ) .. with end return strSub( str, 1, at-1 ) .. with .. strSub( str, at+1, string.len(str) ) end
If you’re a Lua wizard and have a better solution, please share.