lua string之find

 

--计算字符串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'))

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



 

posted on 2015-04-26 17:25  dotdog  阅读(3037)  评论(0编辑  收藏  举报

导航