删除字母之间的引号

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

Remove quotes between letters

问题

在golang中,你可以使用字符串替换函数strings.Replace来删除两个字母之间的引号。以下是修改后的代码示例:

  1. import (
  2. "fmt"
  3. "strings"
  4. )
  5. func RemoveQuotes(s string) string {
  6. s = strings.Replace(s, "\"", "", -1) // 这里我删除了所有的引号,我想要删除的是字母之间的引号
  7. fmt.Println(s)
  8. return s
  9. }
  10. func main() {
  11. var a = "\"bus\"zipcode"
  12. var mockResult = "bus zipcode"
  13. a = RemoveQuotes(a)
  14. if a != mockResult {
  15. fmt.Println("Error or TestRemoveQuotes:", a)
  16. }
  17. }

例如:

""bus"zipcode" = "bus zipcode"

英文:

In golang, how can I remove quotes between two letters, like that:

  1. import (
  2. "testing"
  3. )
  4. func TestRemoveQuotes(t *testing.T) {
  5. var a = "bus\"zipcode"
  6. var mockResult = "bus zipcode"
  7. a = RemoveQuotes(a)
  8. if a != mockResult {
  9. t.Error("Error or TestRemoveQuotes: ", a)
  10. }
  11. }

Function:

  1. import (
  2. "fmt"
  3. "strings"
  4. )
  5. func RemoveQuotes(s string) string {
  6. s = strings.Replace(s, "\"", "", -1) //here I removed all quotes. I'd like to remove only quotes between letters
  7. fmt.Println(s)
  8. return s
  9. }

For example:

> "bus"zipcode" = "bus zipcode"

答案1

得分: 4

你可以使用一个简单的\b"\b正则表达式,它只匹配在前后都有单词边界的双引号:

  1. package main
  2. import (
  3. "fmt"
  4. "regexp"
  5. )
  6. func main() {
  7. var a = "\"test1\",\"test2\",\"test3\""
  8. fmt.Println(RemoveQuotes(a))
  9. }
  10. func RemoveQuotes(s string) string {
  11. re := regexp.MustCompile(`\b"\b`)
  12. return re.ReplaceAllString(s, "")
  13. }

查看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:

  1. package main
  2. import (
  3. "fmt"
  4. "regexp"
  5. )
  6. func main() {
  7. var a = "\"test1\",\"test2\",\"tes\"t3\""
  8. fmt.Println(RemoveQuotes(a))
  9. }
  10. func RemoveQuotes(s string) string {
  11. re := regexp.MustCompile(`\b"\b`)
  12. return re.ReplaceAllString(s, "")
  13. }

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)。不过,如果你想完全删除引号,你可以这样做:

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. func RemoveQuotes(s string) string {
  7. result := ""
  8. arr := strings.Split(s, ",")
  9. for i := 0; i < len(arr); i++ {
  10. sub := strings.Replace(arr[i], "\"", "", -1)
  11. result = fmt.Sprintf("%s,\"%s\"", result, sub)
  12. }
  13. return result[1:]
  14. }
  15. func main() {
  16. a := "\"test1\",\"test2\",\"tes\"t3\""
  17. fmt.Println(RemoveQuotes(a))
  18. }

然而,请注意这种方法效率不高,但我假设这更多是为了学习如何在这种情况下实现。

英文:

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:

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;strings&quot;
  5. )
  6. func RemoveQuotes(s string) string {
  7. result := &quot;&quot;
  8. arr := strings.Split(s, &quot;,&quot;)
  9. for i:=0;i&lt;len(arr);i++ {
  10. sub := strings.Replace(arr[i], &quot;\&quot;&quot;, &quot;&quot;, -1)
  11. result = fmt.Sprintf(&quot;%s,\&quot;%s\&quot;&quot;, result, sub)
  12. }
  13. return result[1:]
  14. }
  15. func main() {
  16. a:= &quot;\&quot;test1\&quot;,\&quot;test2\&quot;,\&quot;tes\&quot;t3\&quot;&quot;
  17. fmt.Println(RemoveQuotes(a))
  18. }

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()添加引号。

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. func main() {
  7. var a = "\"test1\",\"test2\",\"tes\"t3\""
  8. fmt.Println(RemoveQuotes(a))
  9. }
  10. func RemoveQuotes(s string) string {
  11. s = strings.Replace(s, "\"", "", -1) //在这里我删除了所有的引号。我想只删除字母之间的引号
  12. return fmt.Sprintf(`"%s"`, s)
  13. }

链接: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()

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;strings&quot;
  5. )
  6. func main() {
  7. var a = &quot;\&quot;test1\&quot;,\&quot;test2\&quot;,\&quot;tes\&quot;t3\&quot;&quot;
  8. fmt.Println(RemoveQuotes(a))
  9. }
  10. func RemoveQuotes(s string) string {
  11. 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
  12. return fmt.Sprintf(`&quot;%s&quot;`, s)
  13. }

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:

确定