-- 文字列のインデックス、及び、スライス。
getmetatable('').__call = function(str,i,j)
if j==nil then return string.sub(str,i,i) end
if type(i)~='table' then
return string.sub(str,i,j)
else
local t={}
for k,v in ipairs(i) do
t[k]=string.sub(str,v,v)
end
return table.concat(t)
end
end
使い方
test.lua
require "strslice"
local s = "abcdefg"
print(s(3)) --> c
print(s(3, 3)) --> c
print(s(2, 5)) --> bcde
print(s(3, -1)) --> cdefg
print(s(1, -2)) --> abcdef
print(("abc")(3)) --> c