英文:
Remove quotes between letters
问题
在golang中,你可以使用字符串替换函数strings.Replace
来删除两个字母之间的引号。以下是修改后的代码示例:
import (
"fmt"
"strings"
)
func RemoveQuotes(s string) string {
s = strings.Replace(s, "\"", "", -1) // 这里我删除了所有的引号,我想要删除的是字母之间的引号
fmt.Println(s)
return s
}
func main() {
var a = "\"bus\"zipcode"
var mockResult = "bus zipcode"
a = RemoveQuotes(a)
if a != mockResult {
fmt.Println("Error or TestRemoveQuotes:", a)
}
}
例如:
""bus"zipcode" = "bus zipcode"
英文:
In golang, how can I remove quotes between two letters, like that:
import (
"testing"
)
func TestRemoveQuotes(t *testing.T) {
var a = "bus\"zipcode"
var mockResult = "bus zipcode"
a = RemoveQuotes(a)
if a != mockResult {
t.Error("Error or TestRemoveQuotes: ", a)
}
}
Function:
import (
"fmt"
"strings"
)
func RemoveQuotes(s string) string {
s = strings.Replace(s, "\"", "", -1) //here I removed all quotes. I'd like to remove only quotes between letters
fmt.Println(s)
return s
}
For example:
> "bus"zipcode" = "bus zipcode"
答案1
得分: 4
你可以使用一个简单的\b"\b
正则表达式,它只匹配在前后都有单词边界的双引号:
package main
import (
"fmt"
"regexp"
)
func main() {
var a = "\"test1\",\"test2\",\"test3\""
fmt.Println(RemoveQuotes(a))
}
func RemoveQuotes(s string) string {
re := regexp.MustCompile(`\b"\b`)
return re.ReplaceAllString(s, "")
}
查看Go演示,输出为"test1","test2","test3"
。
另请参阅在线正则表达式演示。
英文:
You may use a simple \b"\b
regex that matches a double quote only when preceded and followed with word boundaries:
package main
import (
"fmt"
"regexp"
)
func main() {
var a = "\"test1\",\"test2\",\"tes\"t3\""
fmt.Println(RemoveQuotes(a))
}
func RemoveQuotes(s string) string {
re := regexp.MustCompile(`\b"\b`)
return re.ReplaceAllString(s, "")
}
See the Go demo printing "test1","test2","test3"
.
Also, see the online regex demo.
答案2
得分: 0
在你的示例中,你定义了一个字符串变量,所以外部的引号不是实际字符串的一部分。如果你执行 fmt.Println("bus\"zipcode")
,屏幕上的输出将是 bus"zipcode
。如果你的目标是将字符串中的引号替换为空格,那么你需要用空格而不是空字符串来替换引号 - s = strings.Replace(s, "\"", " ", -1)
。不过,如果你想完全删除引号,你可以这样做:
package main
import (
"fmt"
"strings"
)
func RemoveQuotes(s string) string {
result := ""
arr := strings.Split(s, ",")
for i := 0; i < len(arr); i++ {
sub := strings.Replace(arr[i], "\"", "", -1)
result = fmt.Sprintf("%s,\"%s\"", result, sub)
}
return result[1:]
}
func main() {
a := "\"test1\",\"test2\",\"tes\"t3\""
fmt.Println(RemoveQuotes(a))
}
然而,请注意这种方法效率不高,但我假设这更多是为了学习如何在这种情况下实现。
英文:
In your example you define a string variable so the outer quotes are not part of the actual string. If you would do fmt.Println("bus\"zipcode")
the output on the screen would be bus"zipcode
. If your goal is to replace quotes in a string with a space then you need to replace the quote not with an empty string as you do, but rather with a space - s = strings.Replace(s, "\"", " ", -1)
. Though if you want to remove the quotes entirely you can do something like this:
package main
import (
"fmt"
"strings"
)
func RemoveQuotes(s string) string {
result := ""
arr := strings.Split(s, ",")
for i:=0;i<len(arr);i++ {
sub := strings.Replace(arr[i], "\"", "", -1)
result = fmt.Sprintf("%s,\"%s\"", result, sub)
}
return result[1:]
}
func main() {
a:= "\"test1\",\"test2\",\"tes\"t3\""
fmt.Println(RemoveQuotes(a))
}
Note however that this is not very efficient, but I assume it's more about learning how to do it in this case.
答案3
得分: 0
我不确定你在评论中提到的“我只想引用test3内部”的需求是什么。
这段代码会删除内部的引号,就像你所做的那样,但它会使用fmt.Sprintf()
添加引号。
package main
import (
"fmt"
"strings"
)
func main() {
var a = "\"test1\",\"test2\",\"tes\"t3\""
fmt.Println(RemoveQuotes(a))
}
func RemoveQuotes(s string) string {
s = strings.Replace(s, "\"", "", -1) //在这里我删除了所有的引号。我想只删除字母之间的引号
return fmt.Sprintf(`"%s"`, s)
}
链接:https://play.golang.org/p/dKB9DwYXZp
英文:
I am not sure about what you need when you commented I want to only quote inside test3
.
This code is removing the quotes from the inside, as you did, but it is adding the quotes with fmt.Sprintf()
package main
import (
"fmt"
"strings"
)
func main() {
var a = "\"test1\",\"test2\",\"tes\"t3\""
fmt.Println(RemoveQuotes(a))
}
func RemoveQuotes(s string) string {
s = strings.Replace(s, "\"", "", -1) //here I removed all quotes. I'd like to remove only quotes between letters
return fmt.Sprintf(`"%s"`, s)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论