String extensions
This code modifies the metatable of the string type to add some useful functionality.
local meta = getmetatable("") -- get the string metatable
meta.__add = function(a,b) -- the + operator
return a..b
end
meta.__sub = function(a,b) -- the - operator
return a:gsub(b,"")
end
meta.__mul = function(a,b) -- the * operator
return a:rep(b)
end
meta.__index = function(a,b) -- if you attempt to do string[id]
if type(b) ~= "number" then
return string[b]
end
return a:sub(b,b)
end
Once you've applied this code, you'll be able to do this:
print("foo" + "bar") --> foobar
print("foo the bar" - " the ") --> foobar
print("repetition is repetetive " * 3) --> repetition is repetetive repetition is repetetive repetition is repetetive
mystring = "abc"
print(mystring[2]) --> b