1. You can use the join operator.. To concatenate strings.

  • If the operand contains a numeric value, the Lua language first converts the value to a string.

"Hello" .. "World"   -->  Hello World

"result is" .. 3          -->  result is 3

  • In some languages, string concatenation uses the plus sign, but in lua, 3+5 and 3.. 5 is different.

print(3 + 5)   -->  8

print(3 .. 5) -- - > 35

  • Restrictions on use:

  • Concatenated elements can only be numbers or strings

  • When connecting numbers, there must be a space between them, otherwise an error will be reported. When writing, it is best to ensure that whatever type of connection is made,.. There are Spaces left and right.

print(3.. 5) --> script.lua:2: malformed number near '3.. '

print(3 .. {})  -->    script.lua:2: attempt to concatenate a table value
stack traceback:
	script.lua:2: in main chunk
	[C]: in ?

Exited with error status 1
Copy the code
  • Performance analysis:

Using the operator.. The space corresponding to the old result will be GC in the garbage collection period of Lua at some point. With the increase of result, more new space will be opened up in the future, and copy operation will be carried out to generate more space to be GC, so the performance is reduced.

2. Concatenate strings using the table.concat () function.

  • The table.concat(table, sep, start, end) function lists all the elements in the table array from the start position to the end position, separated by the separator sep.

    Fruits = {” banana “, “orange”, } print(table.concat(fruits)) –> Bananaorangerapple print(table.concat(fruits, “, “)) –> banana,orange,apple Print (table.concat(fruits, “, “, 2, 3)) –> orange, apple

  • Restrictions on use:

  • Concatenation elements must be strings or numbers.

  • You must first put all the elements to be joined into a table, which may be inconvenient to use

  • Source:

  • Concat internally implements tocancat, as follows:

    static const lua_Reg tab_funcs[] = { {“concat”, tconcat}, … }

  • Here’s a brief explanation of the toconcat function

    Static int tconcat (lua_State *L) {// table. Concat is stored on the stack. // Stack bit 1 stores the table // Stack bit 2 stores the sep // Stack bit 3 and 4 stores the start and end positions

    // Sep =" const char *sep = luaL_optlstring(L, 2, "", &lsep); // Sep = const char *sep = luaL_optlstring(L, 2, "", &lsep); Table luaL_checktype(L, 1, LUA_TTABLE); table luaL_checktype(L, 1, LUA_TTABLE); I = luaL_optint(L, 3, 1); // I = luaL_optint(L, 3, 1); // If the end index of the table is nil, the default is the size of the table array part. last = luaL_opt(L, luaL_checkint, 4, luaL_getn(L, 1)); // Use luaV_concat to concatenate the buffbuffinit (L, &b); // Use luaV_concat to concatenate the buffbuffinit (L, &b); // Select * from 'I' and 'last' from 'table' and 'BUFSIZ' and 'last' from 'table' and 'BUFSIZ'; i < last; i++) { addfield(L, &b, i); luaL_addlstring(&b, sep, lsep); If (I == last) addfield(L, &b, I); if (I == last) addfield(L, &b, I); // Put all previously generated tStrings on the stack through luaV_concat (which is.. Merging functions of syntactic sugar) merged into a TString, on the stack, equivalent to a return value, for the upper function taking luaL_pushresult (& b); return 1;Copy the code

    }

  • Performance analysis:

Table. Concat does not allocate memory frequently. Only when a 8192 BUFF is written, a TString will be generated. In large string merges, you should choose this approach whenever possible.

The underlying way to concatenate strings is also using the operator.. , but its use of algorithms reduces the use of operators.. The number of times the GC is reduced, thus improving efficiency.

3. Use the format string.format(” %s%s%s “,str1,str2,str3) to concatenate strings

The Lua String module’s built-in format function, similar to c’s printf, can format different types of data into strings.

  • Format: string.format(FMT, [… )

  • The format string may contain the following escape code:

    %c – Takes a number and converts it to the corresponding character %d in the ASCII code table, % I – Takes a number and converts it to signed integer format %o – takes a number and converts it to octal format %u – takes a number and converts it to unsigned integer format %x – takes a number and converts it to hexadecimal format, Use the lowercase letter %X – to take a number and convert it to the hexadecimal number format, use the uppercase letter %e – to take a number and convert it to the scientific notation format, Use the uppercase letter E %f – Take a number and convert it to floating point format %g(% g) – Take a number and convert it to %E (%E, The shorter format %q – takes a string and converts it to a format that can be safely read by the Lua compiler. %s – takes a string and formats it with the given arguments

  • To further refine the format, you can add parameters after the % sign, which will be read in the following order:

  • Symbol: A + sign means that the numeric escape character following it will make positive numbers show a plus sign. By default, only negative numbers show signs.

  • Placeholder: a 0, used when the string width is specified. The default placeholder for non-zero is space.

  • Align identifier: When the width of the string is specified, the default is right. If you add a – sign, it changes to left.

  • The width of the numerical

  • Decimal place/string cutting: the decimal part n increased after the width value. If f(float escape character, such as %6.3f) is followed by f, only n decimal bits of the float number are preserved. If s(string escape character, such as %5.3s) is followed by s, only the first N bits of the string are displayed.

  • Example:

    string1 = “mynameisbb”; Print (string. Format (“%.4s,%+08d,%.2f”,string1,number1,number2)) –> Myna, + 0123456123.46

  • Performance analysis:

  • Concatenation instructions for strings are safer than formatting because the length of the formatting function string is limited to 512. Connection strings do not have this restriction.

  • The operation of formatting the string is more complex and more expensive.

4.string.rep

The other lua String concatenation module that can do string concatenation is rep, but it is limited to doing a string concatenation N times.

Example:

string.rep("abc",3)   -->  abcabcabc

The performance summary

  • Table.concat () is superior to string concatenation..

  • String concatenators are superior to format strings

Some of the content reference learning blog.csdn.net/fengshenyun…