StringIndexOutOfBoundsException

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

StringIndexOutOfBoundsException

Hugo <Nabble>
The code below works in the lua demo, but doesn't work in Luan (for luan, replace 'string' with 'String').
function endsWith(s,p)
  return string.sub(s, -string.len(p)) == p
end

a = '1234567890'
print(endsWith(a, '890')) -- true
print(endsWith(a, '7890')) -- true
print(endsWith(a, '1890')) -- false
print(endsWith(a, '111111111111111111')) -- false
Reply | Threaded
Open this post in threaded view
|

Re: StringIndexOutOfBoundsException

fschmidt
Administrator
fixed in rev 249

By the way, you can use object oriented syntax here like this:
import "String"

function String.endsWith(s,p)
  return s.sub(-p.len()) == p
end

a = '1234567890'
print(a.endsWith('890')) -- true
print(a.endsWith('7890')) -- true
print(a.endsWith('1890')) -- false
print(a.endsWith('111111111111111111')) -- false
Woe to those who call bad good and good bad -- Isaiah 5:20
Following the Old Testament, not evil modern culture
Reply | Threaded
Open this post in threaded view
|

Re: StringIndexOutOfBoundsException

Hugo <Nabble>
Thanks, it works fine now.

Closing thread.