• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

lua字符串对齐函数

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

  最近要用到字符串对齐,开始只是一部分字符串,就直接加空格了,后来发现有很多,

于是写了个字符串对齐的函数。

--功能:分割字符串
--参数:带分割字符串,分隔符
--返回:字符串表
function string.split(str, delimiter)
    str = tostring(str)
    delimiter = tostring(delimiter)
    if (delimiter=='') then return false end
    local pos,arr = 0, {}
    -- for each divider found
    for st,sp in function() return string.find(str, delimiter, pos, true) end do
        table.insert(arr, string.sub(str, pos, st - 1))
        pos = sp + 1
    end
    table.insert(arr, string.sub(str, pos))
    return arr
end

--功能:统计字符串中字符的个数
--返回:总字符个数、英文字符数、中文字符数
function string.count(str)
  local tmpStr=str
  local _,sum=string.gsub(str,"[^\128-\193]","")
  local _,countEn=string.gsub(tmpStr,"[%z\1-\127]","")
  return sum,countEn,sum-countEn
end
--功能:计算字符串的宽度,这里一个中文等于两个英文
function string.width(str)
  local _,en,cn=string.count(str)
  return cn*2+en
end

-- 功能: 把字符串扩展为长度为len,居中对齐, 其他地方以filledChar补齐
-- 参数: str 需要被扩展的字符、数字、字符串表,len 被扩展成的长度,
--       filledChar填充字符,可以为空
function string.tocenter(str, len, filledChar)
  local function tocenter(str,len,filledChar)
      str = tostring(str);
      filledChar = filledChar or " ";
      local nRestLen = len - string.width(str); -- 剩余长度
      local nNeedCharNum = math.floor(nRestLen / string.width(filledChar)); -- 需要的填充字符的数量
      local nLeftCharNum = math.floor(nNeedCharNum / 2); -- 左边需要的填充字符的数量
      local nRightCharNum = nNeedCharNum - nLeftCharNum; -- 右边需要的填充字符的数量
       
      str = string.rep(filledChar, nLeftCharNum)..str..string.rep(filledChar, nRightCharNum); -- 补齐
      return str
  end
  if type(str)=="number" or type(str)=="string" then
      if not string.find(tostring(str),"\n") then
        return tocenter(str,len,filledChar)
      else
        str=string.split(str,"\n")
      end
  end
  if type(str)=="table" then
    local tmpStr=tocenter(str[1],len,filledChar)
    for i=2,#str do
      tmpStr=tmpStr.."\n"..tocenter(str[i],len,filledChar)
    end
    return tmpStr
  end

end
-- 功能: 把字符串扩展为长度为len,左对齐, 其他地方用filledChar补齐
function string.toleft(str, len, filledChar)
  local function toleft(str, len, filledChar)
    str    = tostring(str);
    filledChar  = filledChar or " ";
    local nRestLen  = len - string.width(str);        -- 剩余长度
    local nNeedCharNum = math.floor(nRestLen / string.width(filledChar)); -- 需要的填充字符的数量
     
    str = str..string.rep(filledChar, nNeedCharNum);     -- 补齐
    return str;
  end
  if type(str)=="number" or type(str)=="string" then
      if not string.find(tostring(str),"\n") then
        return toleft(str,len,filledChar)
      else
        str=string.split(str,"\n")
      end
  end
  if type(str)=="table" then
    local tmpStr=toleft(str[1],len,filledChar)
    for i=2,#str do
      tmpStr=tmpStr.."\n"..toleft(str[i],len,filledChar)
    end
    return tmpStr
  end
end
-- 功能: 把字符串扩展为长度为len,右对齐, 其他地方用filledChar补齐
function string.toright(str, len, filledChar)
  local function toright(str, len, filledChar)
    str    = tostring(str);
    filledChar  = filledChar or " ";
    local nRestLen  = len - string.width(str);        -- 剩余长度
    local nNeedCharNum = math.floor(nRestLen / string.width(filledChar)); -- 需要的填充字符的数量
     
    str = string.rep(filledChar, nNeedCharNum).. str;     -- 补齐
    return str;
  end
  if type(str)=="number" or type(str)=="string" then
      if not string.find(tostring(str),"\n") then
        return toright(str,len,filledChar)
      else
        str=string.split(str,"\n")
      end
  end
  if type(str)=="table" then
    local tmpStr=toright(str[1],len,filledChar)
    for i=2,#str do
      tmpStr=tmpStr.."\n"..toright(str[i],len,filledChar)
    end
    return tmpStr
  end
end

--测试代码
print("对齐测试\n")
print(string.tocenter(string.split("居中cc\n居中","\n"),4*2,"*"))
print(string.tocenter("居中cc\n居中",4*2))
print("\n")
print(string.toright(string.split("居右rr\n居右","\n"),4*2,"*"))
print(string.toright("居右rr\n居右",4*2))
print("\n")
print(string.toleft(string.split("居左ll\n居左","\n"),4*2,"*"))
print(string.toleft("居左ll\n居左",4*2))

 

另外附三个trim(删除控制字符)函数

function string.ltrim(str)
    return string.gsub(str, "^[ \t\n\r]+", "")
end

function string.rtrim(str)
    return string.gsub(str, "[ \t\n\r]+$", "")
end

function string.trim(str)
    str = string.gsub(str, "^[ \t\n\r]+", "")
    return string.gsub(str, "[ \t\n\r]+$", "")
end

其中,string.split、及三个trim均取自quick-cocos2d-x中的functions.lua,

三个对齐函数,修改自网上,以支持中文、字符串表、换行的字符串。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
lua学习笔记——逻辑运算符和三目运算符发布时间:2022-07-22
下一篇:
lua随机数的问题发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap