I have a string which can be of any characters. I like to extract only the part between two exclamation marks or before the first or after the last one:
str = "what)ever!when(ver!time!is!mo/ey"
function getStrPart(str,n) -- return a substring
return str:sub(...) or nil
end
getStrPart(str,0) -- return "what)ever" -- string until the first !
getStrPart(str,1) -- return "when(ver" -- between first and second !
getStrPart(str,2) -- return "time"
getStrPart(str,3) -- return "is"
getStrPart(str,4) -- return "mo/ey"
getStrPart(str,5) -- return nil -- for all n less 0 or > 4 (no of the !)
If the string contains no !
str = "whatever"
then the function should return nil