Count certain char in a string
 

News:

29 December 2022 - PtokaX 0.5.3.0 (20th anniversary edition) released...
11 April 2017 - PtokaX 0.5.2.2 released...
8 April 2015 Anti child and anti pedo pr0n scripts are not allowed anymore on this board!
28 September 2015 - PtokaX 0.5.2.1 for Windows 10 IoT released...
3 September 2015 - PtokaX 0.5.2.1 released...
16 August 2015 - PtokaX 0.5.2.0 released...
1 August 2015 - Crowdfunding for ADC protocol support in PtokaX ended. Clearly nobody want ADC support...
30 June 2015 - PtokaX 0.5.1.0 released...
30 April 2015 Crowdfunding for ADC protocol support in PtokaX
26 April 2015 New support hub!
20 February 2015 - PtokaX 0.5.0.3 released...
13 April 2014 - PtokaX 0.5.0.2 released...
23 March 2014 - PtokaX testing version 0.5.0.1 build 454 is available.
04 March 2014 - PtokaX.org sites were temporary down because of DDOS attacks and issues with hosting service provider.

Main Menu

Count certain char in a string

Started by TTB, 26 April, 2011, 16:18:06

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

TTB

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)
TTB

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

TTB

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
TTB

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

TTB

TTB

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

SMF spam blocked by CleanTalk