【问题标题】:Why is Lua only printing the first word in a string?为什么 Lua 只打印字符串中的第一个单词?
【发布时间】:2020-12-07 20:38:27
【问题描述】:

我正在用 Lua 编写一个发送消息的简单程序,但由于某种原因,它只打印字符串中的第一个单词(例如:'Hello World!' 打印出 'Hello') 这是到目前为止的所有代码:

local msg = (...)
print('Message:',msg)

我是 Lua 的新手,大约一周前开始使用。

【问题讨论】:

    标签: string printing lua


    【解决方案1】:

    当你调用类似的程序时

    lua some_program.lua foo bar baz
    

    那么程序将具有三个单独的字符串参数:

    local a, b, c = ...
    print(a) -- foo
    print(b) -- bar
    print(c) -- baz
    

    所以在你的情况下,(...) 只给你第一个参数。

    有两种方法可以解决这个问题:

    您可以通过引用它来确保仅使用一个长字符串参数调用程序:

    lua some_program.lua "foo bar baz"
    

    或者您可以将通过... 获得的所有参数连接起来。使用table.concat 很容易做到这一点:

    local msg = table.concat({...}, " ")
    print("Message:", msg)
    

    【讨论】:

      猜你喜欢
      • 2015-04-25
      • 1970-01-01
      • 2016-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-28
      • 1970-01-01
      相关资源
      最近更新 更多