Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to concatenate String strings in Lua

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to concatenate String strings in Lua. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Every language will encounter the problem of string concatenation. Last time we talked about string concatenation Concat in C #, we know that many strings in C # generally do not use the "+" sign, because each + operation produces a temporary string. So StringBuilder-- variable string is provided in C # to concatenate, and the final string string will not be generated until the final tostring.

The default is ".." in Lua language. Two English periods to indicate the concatenation of strings. But when we need to concatenate multiple strings, the same way of thinking table.concat

Table.concat (list [, sep [, I [, j])

Lua provides a list where all elements are strings or numbers and returns the string list [I].. seplist [item1] sep..list [j]. The default value for sep is empty string, the default value for I is 1, and the default value for j is # list. If I is greater than j, an empty string is returned.

That means we can put the following code

Function Concat (...)

Local origin = {.}

Local message = ""

For iJournal v in pairs (origin) do

Message = message.. V

End

Return message

End

Change to

Function Concat (...)

Local message = {.}

Return table.concat (message)

End assumes that the file is now being read line by line

-- WARNING: bad code aheadquarter!

Local buff = ""

For line in io.lines () do

Buff = buff.. Line.. "\ n"

End

Lua uses a real garbage collection algorithm; when it detects that the program is using too much memory, it traverses all its data structures and releases those that are no longer in use (garbage).

Let's assume that we are in the middle of the read loop. Buff is already a 50-KB string with 20 bytes per line. When Lua concatenates, buff..line.. "\ n", it will create a new string with 50020 bytes and copy 50 KB buff into the new string. That is, for each new line, Lua moves 50 KB of memory and grows. After reading 100 lines (only 2 KB), Lua has moved more than 5 MB of memory.

Buff = buff.. Line.. "\ n"

After two loops, two old strings make up a total of more than 100 KB of garbage. So Lua quite correctly decided that now was a good time to run its garbage collector, so it released the 100 KB. The problem is that this will happen every two cycles, so Lua will run its garbage collector 2000 times before reading the entire file. Even if all this work is done, its memory usage will be about three times the file size.

With concat, we can simply collect all the strings in one table and concatenate them all at once. Because concat is implemented in C, it works even for large strings.

Then modify the above code.

Local t = {}

For line in io.lines () do

Table.insert (t, line)

End

S = table.concat (t, "\ n").. "\ n" above is how to concatenate the String string in the Lua shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report