如何从Godot的弦中删除所有标点符号?
我正在构建一个命令解析器,并且我成功地设法将字符串拆分为单独的单词并使其全部工作,但是我有点困惑的一件事是如何从字符串中删除所有标点符号。用户将输入类似字符。呢?通常,但是对于那些角色,它无法识别这个词,因此需要删除任何标点符号。
到目前为止,我已经对此进行了测试:
func process_command(_input: String) -> String:
var words:Array = _input.replace("?", "").to_lower().split(" ", false)
它可以正常工作并成功地删除问号,但我希望它删除所有标点符号。希望这将是一个简单的问题!我是Godot的新手,所以仍在学习很多东西如何工作。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过将它们放入数组中,然后做您已经在做的事情来删除一个有意的角色:
我不确定您从长远来看要实现的目标,但是使用正则表达式可以更容易地解析字符串。因此,如果您想搜索字符串以获取Apecific模式,则应该研究一下:
regex
You could remove an unwantes character by putting them in an array and then do what you already are doing:
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
给定一些输入,我将在此处以示例写入:
我们想获得一个修改后的版本,其中我们要过滤这些字符:
我们需要知道要过滤的内容。例如:
我们可以拥有我们接受的事物列表,您只会在以后进行有条件的
。如果是这样,将其添加到输出中:
Given some input, which I'll just write here as example:
We want to get a modified version where we have filtered the characters:
We need to know what we will filter. For example:
We could have a list of things we accept instead, you would just flip a conditional later on.
And then we can iterate over the string, for each character decide if we want to keep it, and if so add it to the output: