Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove all punctuation from a string in Godot?

Tags:

godot

gdscript

I'm building a command parser and I've successfully managed to split strings into separate words and get it all working, but the one thing I'm a bit stumped at is how to remove all punctuation from the string. Users will input characters like , . ! ? often, but with those characters there, it doesn't recognize the word, so any punctuation will need to be removed.

So far I've tested this:

func process_command(_input: String) -> String:
    var words:Array = _input.replace("?", "").to_lower().split(" ", false)

It works fine and successfully removes question marks, but I want it to remove all punctuation. Hoping this will be a simple thing to solve! I'm new to Godot so still learning how a lot of the stuff works in it.

like image 717
Hidden Avatar asked Sep 10 '25 12:09

Hidden


1 Answers

You could remove an unwantes character by putting them in an array and then do what you already are doing:

var str_result = input
var unwanted_chars = [".",",",":","?" ] #and so on

for c in unwanted_chars:
   str_result = str_result.replace(c,"")

I am not sure what you want to achieve in the long run, but parsing strings can be easier with the use of regular expressions. So if you want to search strings for apecific patterns you should look into this: regex

like image 89
Bugfish Avatar answered Sep 13 '25 14:09

Bugfish