Can you overload the operator for a string?

Can I add my own operators and meta methods to strings in Lua?

I want to do something like this:

local str = "test"

print(str[2 ]) --> "e"
print(str()) --> "TEST"
print(-str) --> "tset"
print(str + "er" ) --> "tester"
print(str * 2) --> "testtest"

Yes, using the “secret” metatable of strings in Lua, you can define many different operator overloads for strings.

Please try the following code:

< p>

getmetatable('').__index = function(str,i) return string.sub(str,i,i) endgetmetatable('').__call = function(str,i) return string .upper(str) endgetmetatable('').__unm = function(str,i) return string.reverse(str) endgetmetatable('').__add = function(str,i) return (str .. i) endgetmetatable(' ').__mul = function(str,i) return string.rep(str, i) endlocal str = "test"

print(str[2]) --> "e"
print(str()) --> "TEST"
print(-str) --> "tset"
print(str + "er") --> "tester"
print(str * 2) --> "testtest"

The reason you cannot use setmetatable(“,…) is because it can only be used on tables. But through the above “hack”, you can Easily insert different methods into the string.

If you really need it, remember to only Use it locally in your own code, because there may be conflicts in using it globally in your project.

However, the correct way to manipulate strings or data is to use the method in the module. The meta provided in this example The method is not OOP friendly, and affects all the strings in the range in Lua after it is defined. Lua gives us the ability to edit the string metatable, but we should be responsible for using it.

There is nothing str: sub( ), str: upper() and other methods cannot be used. These examples should be used before changing the meta functions of strings in the entire program.

Is it possible to use your own calculations? To add symbols and meta methods to strings in Lua?

I want to do something like this:

local str = "test"

print(str[2 ]) --> "e"
print(str()) --> "TEST"
print(-str) --> "tset"
print(str + "er" ) --> "tester"
print(str * 2) --> "testtest"

Yes, use strings in Lua The “secret” metatable can define many different operator overloads for strings.

Please try the following code:

getmetatable(' ').__index = function(str,i) return string.sub(str,i,i) endgetmetatable('').__call = function(str,i) return string.upper(str) endgetmetatable('').__unm = function(str,i) return string.reverse(str) endgetmetatable('').__add = function(str,i) return (str .. i) endgetmetatable('').__mul = function(str,i) return string.rep(str, i) endlocal str = "test"

print(str[2]) --> "e"
print(str()) --> "TEST "
print(-str) --> "tset"
print(str + "er") --> "tester"
print(str * 2) --> "testtest"

The reason you can’t use setmetatable(“,…) is because it can only be used on tables. But through the “hack” above, you can easily insert different methods into the string.

If you really need it, remember to only use it locally in your own code, because there may be conflicts in using it globally in your project.

However, manipulating strings or data The correct way is to use the modulus The method in the block. The meta method provided in this example is not OOP friendly, and affects all strings in the range in Lua after definition. Lua gives us the ability to edit the string metatable, but we should be responsible for using it.

There are no examples of str:sub(), str:upper() and other methods that cannot be used. These should be used before changing the meta functions of strings in the entire program.

Leave a Comment

Your email address will not be published.