首页 > 其他 > 详细

lua string之find

时间:2015-04-26 18:07:16      阅读:258      评论:0      收藏:0      [点我收藏+]

 

--计算字符串string中char的个数
--better version
local function count_char(string, char)
  local index = 0
  local count = 0
  while ture do
    index = string.find(string, char, i+1)
    if not index then break end
    count = count + 1
  end
  return count
end

local function count_char(string, char)
  local index = 0
  local count = 0
  while true do
    index = string.find(string, char)
    if not index then break end
    count = count + 1
    string = string.sub(string, index+1, #string)
  end
  return count
end

--test
local s = ‘,hello,world,apple,‘
print(count_char(s, ‘,‘))
print(count_char(s, ‘p‘))

结论:工欲善其事,必先利其器



 

lua string之find

原文:http://www.cnblogs.com/dotdog/p/4458027.html

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