Jump to content
Sign in to follow this  
Eigensheep

Counting characters

Recommended Posts

Eigensheep

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.

Share this post


Link to post
Share on other sites
Eigensheep

Now I feel stupid. :) Thanks for your reply.

Share this post


Link to post
Share on other sites
Siao

StringReplace also works.


"be smart, drink your wine"

Share this post


Link to post
Share on other sites
weaponx

$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

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.