Count specific characters in a string

Get help using Construct 2

Post » Tue Jan 22, 2013 12:45 pm

Hi

I have a string - say "Mg++", and I wand to find out how many "+"'s there is in it..

How would you go about that?

I suspect the use of tokencount, but can't really come up with a solution..

Regards
   Engberg
B
5
Posts: 37
Reputation: 840

Post » Tue Jan 22, 2013 1:28 pm

Does this not work?

tokencount("Mg++", "+")
Moderator
B
72
S
13
G
11
Posts: 900
Reputation: 11,783

Post » Tue Jan 22, 2013 4:06 pm

tokencount("Mg+++", "+") returns 4
tokencount("Mg++", "+") returns 3
tokencount("Mg+", "+") returns 2
tokencount("Mg", "+") returns 1
tokencount("M++", "+") returns 3

Alright, so it seems safe to say, that for my purpose that would work.. Thank you for taking time to answer :-)

Regards
   Engberg
B
5
Posts: 37
Reputation: 840

Post » Tue Jan 22, 2013 7:51 pm

Yeah this is as designed, as the character is considered a separator. So you'd normally use it like this:

tokencount("apples+oranges+bananas", "+")

This equals 3, as it's separated 3 bits of text. The way you're using isn't the intended use, but it should work fine.
Moderator
B
72
S
13
G
11
Posts: 900
Reputation: 11,783

Post » Tue Jan 22, 2013 9:24 pm

As thehen said this isn't the intended use, but if you just remember to subtract 1 from the result you should be good to go.
B
49
S
11
G
10
Posts: 1,833
Reputation: 14,418


Return to How do I....?

Who is online

Users browsing this forum: 99Instances2Go, Colludium, jeffige, LeFuji, plinkie, Radulepy and 0 guests