PtokaX forum

Development Section => Your Developing Problems => Topic started by: TTB on 26 April, 2011, 16:18:06

Title: Count certain char in a string
Post by: TTB on 26 April, 2011, 16:18:06
Hi there,

I wonder if there is a faster... and better way to count a certain character in a string.  In this case, I want to know how many times the "\" is in a string. I've solved it this way, but there should be a better way.... hopefully. Any suggestions? :-)


-- little trick to count depth of folders...
local _,_,weeknrcrap,crap = string.find(b,"Week%s+(%d+)\\(.*)")
local count = 0
while true do
if string.match(crap,"\\") then
s,e,crap = string.find(crap,"\\(.*)")
count = count+1
else
break
end
end
LogLine(hub,"debug",count.." times the '\\' in "..b)
Title: Re: Count certain char in a string
Post by: TTB on 26 April, 2011, 16:37:42
OK... a new thought:
CountSlash = function(data)
   local count = 0
   for slash in string.gmatch(data, "\\")
      count = count + 1
   end
   return count
end


This should be it.... I guess  :P
Title: Re: Count certain char in a string
Post by: TTB on 27 April, 2011, 08:35:39
Thank you Mutor  8)