类似的东西在未来:字符串部分分开;写在一个新的字符串ASCII
str="Hello;this;is;a;text"
我所希望的结果是这样的:
result="72:101:108:108:111;116:104:105:115;..."
这应该是在ASCII文本。
类似的东西在未来:字符串部分分开;写在一个新的字符串ASCII
str="Hello;this;is;a;text"
我所希望的结果是这样的:
result="72:101:108:108:111;116:104:105:115;..."
这应该是在ASCII文本。
查找某个字符或字符串(如“”) - https://www.lua.org/pil/20.1.html
转换字符的ASCII码可以通过string.byte
做 - https://www.lua.org/pil/20.html
你需要做什么可以用string.find
完成使用上面提到的两个函数构建一个新的字符串。如果您需要更多的基于字符串的功能,请访问官方的Lua网站:https://www.lua.org/pil/contents.html
你可以使用字符串匹配度日;
分隔每个单词,然后转换,CONCAT:
local str = "Hello;this;is;a;text"
for word in str:gmatch("[^;]+") do
ascii = table.pack(word:byte(1, -1))
local converted = table.concat(ascii, ":")
print(converted)
end
上述代码的输出是:
72:101:108:108:111
116:104:105:115
105:115
97
116:101:120:116
我会把剩下的工作留给你。提示:使用table.concat
。
这里是另一种方法,它利用这一事实,gsub
接受一个表,它读取替换:
T={}
for c=0,255 do
T[string.char(c)]=c..":"
end
T[";"]=";"
str="Hello;this;is;a;text"
result=str:gsub(".",T):gsub(":;",";")
print(result)
好吧...我得到的方式进一步,但我怎么也找不到返回一个字符串由两个独立的串像
str=str1&" "&str2
Ask一个单独的问题。但你想要[字符串连接运算符](https://www.lua.org/manual/5.3/manual.html#3.4.6)。 – lhf
另一种可能性:
function convert(s)
return (s:gsub('.',function (s)
if s == ';' then return s end
return s:byte()..':'
end)
:gsub(':;',';')
:gsub(':$',''))
end
print(convert 'Hello;this;is;a;text')
'string.gsu b(str,“[^;]”,函数(c)返回tostring(string.byte(c))..':'end)' – moteus