Author Topic: Count certain char in a string  (Read 1746 times)

0 Members and 1 Guest are viewing this topic.

Offline TTB

  • Lord
  • ***
  • Posts: 436
  • Karma: +17/-1
Count certain char in a string
« 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? :-)

Code: [Select]
-- 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)
« Last Edit: 27 April, 2011, 08:35:00 by TTB »
TTB

(? ?.??.-> Admin @ Surfnet hubs <-.??.???)

PtokaX forum

Count certain char in a string
« on: 26 April, 2011, 16:18:06 »

Offline TTB

  • Lord
  • ***
  • Posts: 436
  • Karma: +17/-1
Re: Count certain char in a string
« Reply #1 on: 26 April, 2011, 16:37:42 »
OK... a new thought:
Code: [Select]
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
TTB

(? ?.??.-> Admin @ Surfnet hubs <-.??.???)

Offline TTB

  • Lord
  • ***
  • Posts: 436
  • Karma: +17/-1
Re: Count certain char in a string
« Reply #2 on: 27 April, 2011, 08:35:39 »
Thank you American Idiot  8)
TTB

(? ?.??.-> Admin @ Surfnet hubs <-.??.???)

PtokaX forum

Re: Count certain char in a string
« Reply #2 on: 27 April, 2011, 08:35:39 »