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'))
结论:工欲善其事,必先利其器