2014-01-26 44 views
5

我想一個字符串在Lua添加到返回的值:的Lua:試圖在一個字符串值進行算術

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

print(func("ABC")) 

,我得到一個錯誤:

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

或者這個錯誤(在我原來的程序):

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

我試圖用tosring(STR)+ 「_東西」,但沒有幫助...

那麼如何在Lua中串聯一個字符串?

+3

'getmetatable '' .__ add = function(a,b)return a..b end' –

+1

@EgorSkriptunoff,如果字符串包含數字,則不起作用:'「 1「+」2「'是數字'3',而不是字符串'」12「'。 – lhf

+0

@lhf - Ops!忘了這個奇怪的功能。從字符串自動強制轉換爲其他數據類型相當不自然。 –

回答

13

看到 「串聯」 在this linkhttp://lua-users.org/wiki/StringsTutorial

的解決方案是使用..,如示例:

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

print(func("HELLO")) 

多數民衆贊成應該返回:

HELLO WORLD

+1

所以你自己做了。榮譽 –