+2 votes

Like in "M a r c u s" string has 5 spaces, ASCII of space is 32, "Potato" has 2 "o". How we do this?

I tried doing with ASCII and a for loop, couldn't make it work.

in Engine by (376 points)

Might be redundant, but this is linked question to my original problem before:


2 Answers

+4 votes
Best answer

The count() function on strings should work.


print("M a r c u s".count(" ")) will return and output 5

It's a lot simpler than you were trying!

by (399 points)
selected by
+1 vote

Like you said, a for loop works:

var count = 0
for character in "Potato":
    if character == "o":
        count += 1

Or you can use a regex. I recommend learning about regexes if you want to do more advanced string processing.

var regex = RegEx.new()
var count = regex.search_all("Potato").size()

I hope this helps!

by (54 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.