string 如何将大的 lua 字符串分解成小的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6795213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 01:10:33  来源:igfitidea点击:

How to break a big lua string into small ones

stringluamultilineliterals

提问by bratao

I have a big string (a base64 encoded image) and it is 1050 characters long. How can I append a big string formed of small ones, like this in C

我有一个大字符串(base64 编码图像),长度为 1050 个字符。我怎样才能附加一个由小字符串组成的大字符串,就像在 C 中这样

 function GetIcon()
    return "Bigggg string 1"\
"continuation of string"\
"continuation of string"\
"End of string"

回答by crashmstr

According to Programming in Lua 2.4 Strings:

根据Lua 2.4 字符串中的编程

We can delimit literal strings also by matching double square brackets [[...]]. Literals in this bracketed form may run for several lines, may nest, and do not interpret escape sequences. Moreover, this form ignores the first character of the string when this character is a newline. This form is especially convenient for writing strings that contain program pieces; for instance,

我们也可以通过匹配双方括号 [[...]] 来分隔文字字符串。这种括号形式的文字可能会运行多行,可能会嵌套,并且不会解释转义序列。此外,当该字符是换行符时,这种形式会忽略字符串的第一个字符。这种形式对于编写包含程序片段的字符串特别方便;例如,

page = [[
<HTML>
<HEAD>
<TITLE>An HTML Page</TITLE>
</HEAD>
<BODY>
 <A HREF="http://www.lua.org">Lua</A>
 [[a text between double brackets]]
</BODY>
</HTML>
]]

This is the closest thing to what you are asking for, but using the above method keeps the newlines embedded in the string, so this will not work directly.

这是与您要求的最接近的事情,但使用上述方法将换行符嵌入字符串中,因此这不会直接起作用。

You can also do this with string concatenation (using ..):

您也可以使用字符串连接(使用 ..)来做到这一点:

value = "long text that" ..
      " I want to carry over" ..
      "onto multiple lines"

回答by legends2k

Most answers here solves this issue at run-time and not at compile-time.

这里的大多数答案都在运行时解决了这个问题,而不是在编译时。

Lua 5.2 introduces the escape sequence \zto solve this problem elegantly without incurring any run-time expense.

Lua 5.2 引入了转义序列\z来优雅地解决这个问题,而不会产生任何运行时开销。

> print "This is a long \z
>>                                string with \z
>>      breaks in between, \z
>> and is spanning multiple lines \z
>> but still is a single string only!"
This is a long string with breaks in between, and is spanning multiple lines but still is a single string only!

\zskips all subsequent characters in a string literal1until the first non-space character. This works for non-multiline literal text too.

\z跳过字符串文字1中的所有后续字符,直到第一个非空格字符。这也适用于非多行文字文本。

> print "This is a simple \z                string"
This is a simple string

From Lua 5.2 Reference Manual

来自Lua 5.2 参考手册

The escape sequence '\z' skips the following span of white-space characters, including line breaks; it is particularly useful to break and indent a long literal string into multiple lines without adding the newlines and spaces into the string contents.

转义序列 '\z' 跳过以下空白字符范围,包括换行符;将长文本字符串拆分和缩进为多行而不在字符串内容中添加换行符和空格特别有用。

1: All escape sequences, including \z, work only on short literal strings("…", '…') and, understandably, not on long literal strings([[...]], etc.)

1:所有转义序列,包括\z,仅适用于短文字串( "…", '…'),并且可以理解,不适用于长文字串( [[...]], 等)

回答by jpjacobs

I'd put all chunks in a table and use table.concaton it. This avoids the creation of new strings at every concatenation. for example (without counting overhead for strings in Lua):

我会把所有的块放在一张桌子上并table.concat在上面使用。这避免了在每次连接时创建新字符串。例如(不计算 Lua 中字符串的开销):

             -- bytes used
foo="1234".. --          4  = 4
    "4567".. -- 4  + 4 + 8  = 16
    "89ab"   -- 16 + 4 + 12 = 32
             -- |    |    |    \_ grand total after concatenation on last line
             -- |    |    \_ second operand of concatenation
             -- |    \_ first operand of concatenation
             -- \_ total size used until last concatenation

As you can see, this explodes pretty rapidly. It's better to:

正如你所看到的,这爆炸得非常快。最好是:

foo=table.concat{
"1234",
"4567",
"89ab"}

Which will take about 3*4+12=24 bytes.

这将需要大约 3*4+12=24 个字节。

回答by A. K.

Have you tried the string.sub(s, i [, j]) function. You may like to look here:

您是否尝试过 string.sub(s, i [, j]) 函数。你可能喜欢看这里:

http://lua-users.org/wiki/StringLibraryTutorial

http://lua-users.org/wiki/StringLibraryTutorial

回答by Nicol Bolas

This:

这个:

    return "Bigggg string 1"\
"continuation of string"\
"continuation of string"\
"End of string"

C/C++ syntax causes the compiler to see it all as one large string. It is generally used for readability.

C/C++ 语法使编译器将其全部视为一个大字符串。它通常用于可读性。

The Lua equivalent would be:

Lua 等效项是:

    return "Bigggg string 1" ..
"continuation of string" ..
"continuation of string" ..
"End of string"

Do note that the C/C++ syntax is compile-time, while the Lua equivalent likely does the concatenation at runtime (though the compiler could theoretically optimize it). It shouldn't be a big deal though.

请注意,C/C++ 语法是编译时的,而 Lua 等价物可能会在运行时进行连接(尽管编译器理论上可以对其进行优化)。不过应该没什么大不了的。