如何从Godot的弦中删除所有标点符号?

发布于 2025-02-13 02:26:52 字数 375 浏览 1 评论 0原文

我正在构建一个命令解析器,并且我成功地设法将字符串拆分为单独的单词并使其全部工作,但是我有点困惑的一件事是如何从字符串中删除所有标点符号。用户将输入类似字符。呢?通常,但是对于那些角色,它无法识别这个词,因此需要删除任何标点符号。

到目前为止,我已经对此进行了测试:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

○愚か者の日 2025-02-20 02:26:52

您可以通过将它们放入数组中,然后做您已经在做的事情来删除一个有意的角色:

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

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

我不确定您从长远来看要实现的目标,但是使用正则表达式可以更容易地解析字符串。因此,如果您想搜索字符串以获取Apecific模式,则应该研究一下:
regex

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

跨年 2025-02-20 02:26:52

给定一些输入,我将在此处以示例写入:

var input := "Hello, It's me!!"

我们想获得一个修改后的版本,其中我们要过滤这些字符:

var output := ""

我们需要知道要过滤的内容。例如:

var deny_list := [",", "!"]

我们可以拥有我们接受的事物列表,您只会在以后进行有条件的

。如果是这样,将其添加到输出中:

for position in input.length():
    var current_character := input[position]
    if not deny_list.has(current_character):
        output += current_character

Given some input, which I'll just write here as example:

var input := "Hello, It's me!!"

We want to get a modified version where we have filtered the characters:

var output := ""

We need to know what we will filter. For example:

var deny_list := [",", "!"]

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:

for position in input.length():
    var current_character := input[position]
    if not deny_list.has(current_character):
        output += current_character
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文