删除字母之间的引号

huangapple go评论156阅读模式
英文:

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(&quot;bus\&quot;zipcode&quot;) the output on the screen would be bus&quot;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, &quot;\&quot;&quot;, &quot; &quot;, -1). Though if you want to remove the quotes entirely you can do something like this:

package main

import (
    &quot;fmt&quot;
    &quot;strings&quot;
)

func RemoveQuotes(s string) string {
	result := &quot;&quot;
	arr := strings.Split(s, &quot;,&quot;)
	for i:=0;i&lt;len(arr);i++ {
	   sub := strings.Replace(arr[i], &quot;\&quot;&quot;, &quot;&quot;, -1)
	   result = fmt.Sprintf(&quot;%s,\&quot;%s\&quot;&quot;, result, sub)
	}
    
	return result[1:]
}

func main() {
	a:= &quot;\&quot;test1\&quot;,\&quot;test2\&quot;,\&quot;tes\&quot;t3\&quot;&quot;
	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 (
	&quot;fmt&quot;
	&quot;strings&quot;
)

func main() {
	var a = &quot;\&quot;test1\&quot;,\&quot;test2\&quot;,\&quot;tes\&quot;t3\&quot;&quot;
	fmt.Println(RemoveQuotes(a))
}

func RemoveQuotes(s string) string {
	s = strings.Replace(s, &quot;\&quot;&quot;, &quot;&quot;, -1) //here I removed all quotes. I&#39;d like to remove only quotes between letters

	return fmt.Sprintf(`&quot;%s&quot;`, s)
}

https://play.golang.org/p/dKB9DwYXZp

huangapple
  • 本文由 发表于 2017年6月6日 03:43:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/44376517.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定