Jappards
Jappards

Reputation: 13

The opposide of string.find() in Lua

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

Answers (1)

Etan Reisner
Etan Reisner

Reputation: 81052

The string.sub function does what you want here.

string.sub (s, i [, j])

Returns the substring of s that starts at i and continues until j; i and j can be negative. If j is absent, then it is assumed to be equal to -1 (which is the same as the string length). In particular, the call string.sub(s,1,j) returns a prefix of s with length j, and string.sub(s, -i) returns a suffix of s with length i.

Upvotes: 4

Related Questions