zoukankan      html  css  js  c++  java
  • lua字符串对齐函数

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

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

    --功能:分割字符串
    --参数:带分割字符串,分隔符
    --返回:字符串表
    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,"[%z1-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),"
    ") then
            return tocenter(str,len,filledChar)
          else
            str=string.split(str,"
    ")
          end
      end
      if type(str)=="table" then
        local tmpStr=tocenter(str[1],len,filledChar)
        for i=2,#str do
          tmpStr=tmpStr.."
    "..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),"
    ") then
            return toleft(str,len,filledChar)
          else
            str=string.split(str,"
    ")
          end
      end
      if type(str)=="table" then
        local tmpStr=toleft(str[1],len,filledChar)
        for i=2,#str do
          tmpStr=tmpStr.."
    "..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),"
    ") then
            return toright(str,len,filledChar)
          else
            str=string.split(str,"
    ")
          end
      end
      if type(str)=="table" then
        local tmpStr=toright(str[1],len,filledChar)
        for i=2,#str do
          tmpStr=tmpStr.."
    "..toright(str[i],len,filledChar)
        end
        return tmpStr
      end
    end
    
    --测试代码
    print("对齐测试
    ")
    print(string.tocenter(string.split("居中cc
    居中","
    "),4*2,"*"))
    print(string.tocenter("居中cc
    居中",4*2))
    print("
    ")
    print(string.toright(string.split("居右rr
    居右","
    "),4*2,"*"))
    print(string.toright("居右rr
    居右",4*2))
    print("
    ")
    print(string.toleft(string.split("居左ll
    居左","
    "),4*2,"*"))
    print(string.toleft("居左ll
    居左",4*2))

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

    function string.ltrim(str)
        return string.gsub(str, "^[ 	
    
    ]+", "")
    end
    
    function string.rtrim(str)
        return string.gsub(str, "[ 	
    
    ]+$", "")
    end
    
    function string.trim(str)
        str = string.gsub(str, "^[ 	
    
    ]+", "")
        return string.gsub(str, "[ 	
    
    ]+$", "")
    end

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

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

  • 相关阅读:
    OpenStack 数据库操作 demo
    python 实现获取电脑IP、主机名、Mac地址
    openvswitch BFD 简介
    Python 获取主机名
    OpenvSwitch完全使用手册
    ovs datapath笔记
    openstack 实用命令
    表示数值的字符串 牛客网 剑指Offer
    反转单词顺序列 牛客网 剑指Offer
    第一个只出现一次字符的位置 牛客网 剑指Offer
  • 原文地址:https://www.cnblogs.com/xdao/p/lua_string_function.html
Copyright © 2011-2022 走看看