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

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

--功能:分割字符串
--参数:带分割字符串,分隔符
--返回:字符串表
function string.split(str, delimiter)
str = tostring(str)
delimiter = tostring(delimiter)
if (delimiter=='') then return false end
local pos,arr = , {}
-- 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 - ))
pos = sp +
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*+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 / ); -- 左边需要的填充字符的数量
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[],len,filledChar)
for i=,#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[],len,filledChar)
for i=,#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[],len,filledChar)
for i=,#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"),*,"*"))
print(string.tocenter("居中cc\n居中",*))
print("\n")
print(string.toright(string.split("居右rr\n居右","\n"),*,"*"))
print(string.toright("居右rr\n居右",*))
print("\n")
print(string.toleft(string.split("居左ll\n居左","\n"),*,"*"))
print(string.toleft("居左ll\n居左",*))

另外附三个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,

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

最新文章

  1. Android Weekly Notes Issue #219
  2. ASP.NET MVC之国际化(十一)
  3. sshfs 实现普通用户可读写
  4. Linux下的tmpfs文件系统(/dev/shm)
  5. iOS用的aes
  6. [收藏夹整理]OpenCV部分
  7. VS2010开发环境最佳字体及配色方法
  8. mysql拒绝访问(Error 1044/1045)问题的解决
  9. Docker 中国官方镜像加速
  10. unity用PUN进行信息交互模块
  11. IIS中 flv、swf 文件无法播放
  12. C++实现对文件中各单词词频的统计及其代码优化
  13. windows server 修改远程桌面连接端口号
  14. C语言编程比赛WBS
  15. CentOS 6.5 升级内核
  16. python 2.X 和 3.X 的区别汇总
  17. 【Oracle】详解Oracle中NLS_LANG变量的使用
  18. jq expando && $.data()
  19. keras Lambda 层
  20. Sharepoint安装的几处注意事项

热门文章

  1. by,with
  2. 那些年困扰我们的Linux 的蠕虫、病毒和木马
  3. beyond compare 比较Xls文件时只显示有差异的列
  4. (转)使用百度 BAE做SVN服务器
  5. [Functional Programming] Monad
  6. Java内存溢出的详细解决方案(转http://developer.51cto.com/art/200906/129346.htm)
  7. vue - 路由传递参数
  8. 指定安装应用程序移至SD卡(App2SD)
  9. 点击div和某些控件之外的地方隐藏div,点击div不隐藏。对象 click和document click冲突有关问题
  10. PLSQL Split分割字符串