String 用线分隔的线部件;以新字符串写入ASCII的步骤

String 用线分隔的线部件;以新字符串写入ASCII的步骤,string,lua,ascii,String,Lua,Ascii,类似的事情即将出现: str="Hello;this;is;a;text" 我想要的结果是: result="72:101:108:108:111;116:104:105:115;..." 应该是ASCII格式的文本。查找某些字符或字符串,例如;可以使用string.find- 可以通过string.byte- 您需要做的是使用上面提到的两个函数构建一个新字符串。如果您需要更多基于字符串的函数,请访问Lua官方网站:查找某些字符或字符串,例如;可以使用string.find- 可以通过str

类似的事情即将出现:

str="Hello;this;is;a;text"
我想要的结果是:

result="72:101:108:108:111;116:104:105:115;..."

应该是ASCII格式的文本。

查找某些字符或字符串,例如;可以使用string.find-

可以通过string.byte-


您需要做的是使用上面提到的两个函数构建一个新字符串。如果您需要更多基于字符串的函数,请访问Lua官方网站:

查找某些字符或字符串,例如;可以使用string.find-

可以通过string.byte-


您需要做的是使用上面提到的两个函数构建一个新字符串。如果您需要更多基于字符串的函数,请访问Lua官方网站:

您可以使用字符串匹配将每个单词分隔为;然后转换,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。

您可以使用字符串匹配将每个单词分隔为;然后转换,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)

下面是另一种方法,它利用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

好的…我有更进一步的方法,但我找不到如何返回由两个独立字符串组成的字符串,如

str=str1&" "&str2
另一种可能性:

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')
另一种可能性:

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')

非常感谢…这将有助于进行下一步…正如我提到的…我是luaOkay的新手…我走得更远,但我找不到如何返回由两个单独字符串组成的字符串,如str=str1&&str2。非常感谢…这将有助于进行下一步…正如我提到的…我是luaOkay的新手…我走得更远,但是我找不到如何返回由两个独立字符串组成的字符串,如str=str1&&str2string.gsubstr,[^;],functionc return-tostringstring.bytec..:“endstring.gsubstr,[^;],functionc return-tostringstring.bytec..”:“end问一个单独的问题。但你想知道答案。请另外问一个问题。但是你想要这个。