首页 > 其他 > 详细

lua脚本分解字符串

时间:2017-08-23 09:18:30      阅读:225      评论:0      收藏:0      [点我收藏+]

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脚本分解字符串

原文:http://www.cnblogs.com/zwyAndDong/p/7416412.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!