+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:

https://godotengine.org/qa/69724/get_visible_characters-count-spaces-possibly-godot3-stable

2 Answers

+3 votes
Best answer

The count() function on strings should work.

e.g:

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
print(count)

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

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

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.