Reputation: 13
The function string.find(char)
returns the first occurrence of the pattern in char
, but is there any way I can do the reverse: give the position as an int and return the character/number that is there and else returns nil
.
I have searched for it if any function exists in the official libraries of the Lua programming language, no results.
I want to make a program which converts a formula of a substance(chemistry) like glucose for example, to the mass in units(u) it has, that is why I need a way to look what is next to the symbol and look if what lies next to the symbol, is a number.
Upvotes: 1
Views: 314
Reputation: 81052
The string.sub
function does what you want here.
string.sub (s, i [, j])
Returns the substring of
s
that starts ati
and continues untilj
;i
andj
can be negative. Ifj
is absent, then it is assumed to be equal to-1
(which is the same as the string length). In particular, the callstring.sub(s,1,j)
returns a prefix ofs
with lengthj
, andstring.sub(s, -i)
returns a suffix ofs
with lengthi
.
Upvotes: 4