码迷,mamicode.com
首页 > 其他好文 > 详细

lua string之find

时间:2015-04-26 18:09:20      阅读:125      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!