+1 vote

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.

asked May 6 in Engine by The_Black_Chess_King (281 points)

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


2 Answers

+3 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!

answered May 6 by RedBlueCarrots (324 points)
selected Sep 5 by The_Black_Chess_King
+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!

answered May 6 by im_not_a_robot (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 How to use this Q&A? before posting your first questions.