Split string on delimiter

I have a type string first part;second part. I want to break it into ;and return the second part. Everything works perfectly:

start = mystring:find(';')
result = mystring:sub(start)

But I was hoping to do this on one line:

result = mystring:sub(mystring:find(';'))

It does not throw an error, but does not return the expected result. Not a big problem, since it works fine on two lines of code, but understanding why it doesn't work on oneliner will help me better understand how lua works.

+5
source share
3 answers

This will also work:

result = mystring:sub((mystring:find(';')))

Additional parentheses ensure that subonly one argument is called, so it will use the default (end mystring) for the second argument.

+1
source

find , , , . 11.
sub, , 11, ";".

+3

Try the following:

s="first part;second part"  
print(s:match(";(.-)$"))

or that:

print(s:sub(s:find(";")+1,-1))
+3
source

All Articles