Lua: attempt to perform arithmetic on a string value

假装没事ソ 提交于 2019-12-01 02:15:22

问题


I'm trying to add a string to a returned value in lua:

local function func(str)
   return (str+"_something")
end

print(func("ABC"))

and I'm getting an error:

"attempt to perform arithmetic on local 'str' (a string value)"

or this error (in my original program):

@user_script:1: user_script:1: attempt to perform arithmetic on a string value

I tried to use tosring(str)+"_something" but didn't help...

so how to Concatenate a string in Lua ?


回答1:


see "Concatenation" in this link: http://lua-users.org/wiki/StringsTutorial

The solution is to use the .., as in example:

local function func(str)
   return (str.." WORLD")
end

print(func("HELLO"))

that's should return:

HELLO WORLD



来源:https://stackoverflow.com/questions/21362374/lua-attempt-to-perform-arithmetic-on-a-string-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!