lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On Thursday 13 October 2005 13:04, Jose Marin wrote:
> Thinking on a more esay to use than a fast solution,
> how hard would be to create a "String" class in Lua,
> with the operator [] overloaded?

With Lua 5.1, I believe that the 'string' table is the default metatable for 
strings. So:

function string:__index(s, i)
  if (type(i) == "number") then
    return string.substr(s, i, i)
  return nil

...might do what you want. (Untested. Never used Lua 5.1.)

+- David Given --McQ-+ "USER'S MANUAL VERSION 1.0:  The information
|    | presented in this publication has been carefully
| ( | for reliability." --- anonymous computer hardware
+- --+ manual

Attachment: pgp4PMbDL1MwB.pgp
Description: PGP signature