【发布时间】:2023-04-07 19:59:01
【问题描述】:
Go 中是否有任何方法或具有正则表达式,它将仅删除字符串中使用的文章?
我尝试了下面的代码,但它也会从字符串中删除其他单词,我在下面显示代码:
removalString := "This is a string"
stringToRemove := []string{"a", "an", "the", "is"}
for _, wordToRemove := range stringToRemove {
removalString = strings.Replace(removalString, wordToRemove, "", -1)
}
space := regexp.MustCompile(`\s+`)
trimedExtraSpaces := space.ReplaceAllString(removalString, " ")
spacesCovertedtoDashes := strings.Replace(trimedExtraSpaces, " ", "-", -1)
slug := strings.ToLower(spacesCovertedtoDashes)
fmt.Println(slug)
已编辑
在此它将删除is 中使用的this。
预期的输出是this-string
【问题讨论】:
-
如果您的请求是针对现有实现的,那是题外话,因为此处不允许资源请求。如果您的问题是如何改进或修复您当前的代码,您需要更具体:您面临什么问题?您需要什么帮助?
-
显示输入和预期输出的示例以及失败的原因。
-
还要注意“is”不是冠词,而是动词。