Eigensheep Posted May 20, 2008 Share Posted May 20, 2008 I feel really silly asking this, especially considering there's a help file, but I can't seem to find out how to count the number of occurences of a specific character.What I would like to do is check a string, for example (4+5)+(3+2) for the number of occurences of the "+" character, and then I can cycle through each one using StringInStr($string,"+",$occurence) to get the location and then check if it is inside brackets or not.So without knowing how many there are, the only way I can think of to do this is by going through every character one by one, which in long strings is inappropriate. Can someone help me please?Thanks in advance. Link to comment Share on other sites More sharing options...
Eigensheep Posted May 20, 2008 Author Share Posted May 20, 2008 Now I feel stupid. Thanks for your reply. Link to comment Share on other sites More sharing options...
Siao Posted May 20, 2008 Share Posted May 20, 2008 StringReplace also works. "be smart, drink your wine" Link to comment Share on other sites More sharing options...
weaponx Posted May 20, 2008 Share Posted May 20, 2008 $string = "(4+5)+(3+2)" $count = CharCount($string, "+") ConsoleWrite("COUNT: " & $count & @CRLF) Func CharCount($sString, $cChar) Local $array = StringRegExp($sString, "\Q" & $cChar & "\E", 3) If IsArray($array) Then Return Ubound($array) Else Return 0 EndIf EndFunc Link to comment Share on other sites More sharing options...
zorphnog Posted May 20, 2008 Share Posted May 20, 2008 (edited) Or... $text="(4+5)+(3+2)" $aResult=StringRegExp($text, "\+", 4) If Not @error Then MsgBox(0,"Occurences", UBound($aResult)) Edit: Oops too late Edited May 20, 2008 by zorphnog Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now