string Lua - 将字符串转换为表格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20423406/
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
Lua - convert string to table
提问by user3074258
I want to convert string text to table and this text must be divided on characters. Every character must be in separate value of table, for example:
我想将字符串文本转换为表格,并且此文本必须按字符进行划分。每个字符都必须在 table 的单独值中,例如:
- a="text"
- --converting string (a) to table (b)
- --show table (b)
- b={'t','e','x','t'}
- 一个=“文本”
- -- 将字符串 (a) 转换为表 (b)
- --显示表(b)
- b={'t','e','x','t'}
回答by moteus
You could use string.gsub function
你可以使用 string.gsub 函数
t={}
str="text"
str:gsub(".",function(c) table.insert(t,c) end)
回答by Oleg V. Volkov
Just index each symbol and put it at same position in table.
只需索引每个符号并将其放在表格中的相同位置。
local str = "text"
local t = {}
for i = 1, #str do
t[i] = str:sub(i, i)
end
回答by Philipp Gesang
The builtin stringlibrary treats Lua strings as byte arrays. An alternative that works on multibyte (Unicode) characters is the unicodelibrarythat originated in the Selene project. Its main selling point is that it can be used as a drop-in replacement for the string library, making most string operations “magically” Unicode-capable.
内置字符串库将 Lua 字符串视为字节数组。另一种适用于多字节 (Unicode) 字符的替代方法是源自 Selene 项目的 unicode库。它的主要卖点是它可以用作字符串库的直接替代品,使大多数字符串操作“神奇地”支持 Unicode。
If you prefer not to add third party dependencies your task can easily be implemented using LPeg. Here is an example splitter:
如果您不想添加第三方依赖项,则可以使用LPeg轻松实现您的任务。这是一个示例拆分器:
local lpeg = require "lpeg"
local C, Ct, R = lpeg.C, lpeg.Ct, lpeg.R
local lpegmatch = lpeg.match
local split_utf8 do
local utf8_x = R"81"
local utf8_1 = R"tests = {
en = [[Lua (/?lu??/ LOO-?, from Portuguese: lua [?lu.(w)?] meaning moon; ]]
.. [[explicitly not "LUA"[1]) is a lightweight multi-paradigm programming ]]
.. [[language designed as a scripting language with "extensible ]]
.. [[semantics" as a primary goal.]],
ru = [[Lua ([лу?а], порт. ?луна?) — интерпретируемый язык программирования, ]]
.. [[разработанный подразделением Tecgraf Католического университета ]]
.. [[Рио-де-Жанейро.]],
gr = [[Η Lua ε?ναι μια ελαφρ? προστακτικ? γλ?σσα προγραμματισμο?, που ]]
.. [[σχεδι?στηκε σαν γλ?σσα σεναρ?ων με κ?ριο σκοπ? τη δυνατ?τητα ]]
.. [[επ?κταση? τη? σημασιολογ?α? τη?.]],
XX = ">5< invalid"
}
-------------------------------------------------------------------------------
local limit = 14
for lang, str in next, tests do
io.write "\n"
io.write (string.format ("<%s %3d> ->", lang, #str))
local chars = split_utf8 (str)
if not chars then
io.write " INVALID!"
else
io.write (string.format (" <%3d>", #chars))
for i = 1, #chars > limit and limit or #chars do
io.write (string.format (" %q", chars [i]))
end
end
end
io.write "\n"
07"
local utf8_2 = R"43" * utf8_x
local utf8_3 = R"49" * utf8_x * utf8_x
local utf8_4 = R"04" * utf8_x * utf8_x * utf8_x
local utf8 = utf8_1 + utf8_2 + utf8_3 + utf8_4
local split = Ct (C (utf8)^0) * -1
split_utf8 = function (str)
str = str and tostring (str)
if not str then return end
return lpegmatch (split, str)
end
end
This snippet defines the function split_utf8()
that creates a table
of UTF8 characters (as Lua strings), but returns nil
if the string
is not a valid UTF sequence.
You can run this test code:
此代码段定义了split_utf8()
创建 UTF8 字符表(作为 Lua 字符串)的函数,但nil
如果字符串不是有效的 UTF 序列,则返回。您可以运行此测试代码:
library method time in ms
string table.insert() 380
string t [#t + 1] = c 310
string gmatch & for loop 280
slnunicode table.insert() 220
slnunicode t [#t + 1] = c 200
slnunicode gmatch & for loop 170
lpeg Ct (C (...)) 70
Btw., building a table with LPeg is significantly faster than calling
table.insert()
repeatedly.
Here are stats for splitting the whole of Gogol's Dead Souls(in
Russian, 1023814 bytes raw, 571395 characters UTF) on my machine:
顺便说一句,使用 LPeg 构建表比table.insert()
重复调用要快得多
。以下是在我的机器上拆分整个果戈理的死魂(俄语,原始 1023814 字节,UTF 571395 个字符)的统计数据:
> t = {} >str = "text" > for i=1, string.len(str) do t[i]= (string.sub(str,i,i)) end > for k , v in pairs(t) do print(k,v) end 1 t 2 e 3 x 4 t >
回答by pygaur
You can below code to achieve this easily.
您可以在下面的代码中轻松实现这一点。
##代码##string.substring.sub(s, i [, j])
Return a substring of the string passed. The substring starts at i. If the third argument j is not given, the substring will end at the end of the string. If the third argument is given, the substring ends at and includes j.
string.substring.sub(s, i [, j])
返回传递的字符串的子字符串。子串从 i 开始。如果未给出第三个参数 j,则子字符串将在字符串的末尾结束。如果给出第三个参数,则子字符串以 j 结尾并包含 j。