lua脚本分解字符串
Posted 行走在0和1之间
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lua脚本分解字符串相关的知识,希望对你有一定的参考价值。
local str="一aA 二b B文字b abc文字 abc " --文字,英文数字和空格分开
local myTable={}
local pointer=0
local slen=string.len(str) --判断字符串的长度
print(slen)
local m=1;
local pos=1;
while(true)
do
--判断是否是最后一个
if(m>slen) then
if(pos<=m-1) then
print(m .. ":" ..string.sub(str,pos,m-1))
end
print("m" .. ":" .. m)
break
end
local bi=string.byte(str,m,m)
if(bi>127) then --大于127是文字
if(pos<=m-1) then --如果是英文或者数字会执行m=m+1,当pos<=m-1时就说明当前不是数字,所以取pos到m-1区间的东西作为一个单词
print(m .. ":" .. string.sub(str,pos,m-1))
end
print(m .. ":" .. string.sub(str,m,m+1))
pos=m+2
m=m+2
elseif(bi==32) then --32为空格
if(pos<=m-1) then
print(m .. ":" .. string.sub(str,pos,m-1))
end
pos=m+1
m=m+1
else
m=m+1
end
end
以上是关于lua脚本分解字符串的主要内容,如果未能解决你的问题,请参考以下文章
请问各位lua大侠,如何用lua脚本读入一个txt文件,并替换里面的字符串呢