简体   繁体   English

为什么string.Replace在Golang中不起作用

[英]Why the string.Replace is not working in golang

I'm making a program to remove the letters from a string if they exsists. 我正在编写一个程序,以删除字符串中存在的字母。 But the expected result will not come. 但是预期的结果不会到来。 The Program I have tried is below:- 我尝试过的程序如下:

package main

import (
  "fmt"
  "strings"
)

func main() {
  strValue := "This is a string"
  stringRemove := []string{"a", "an"}
  var removalString string
  for _, wordToRemove := range stringRemove {
      removalString = strings.Replace(strValue, wordToRemove, "", -1)
  }
  fmt.Println(removalString)
  result := strings.Replace(strValue, " ", "", -1)
  result1 := strings.ToLower(result)
  fmt.Println(result1)
}

Output:- 输出:-

This is a string
thisisastring

If I use the line fmt.Println(removalString) in the for loop then it will print the result:- 如果我在for循环中使用fmt.Println(removalString)行,它将打印结果:

output:- 输出:-

This is  string
This is a string
This is a string
thisisastring

Expected output:- 预期产量:-

thisisstring

kheedn li link kheedn li链接

You always apply the replace operation on the original string strValue , so after the loop only the last removable word will be removed (which is not even contained in your example). 您总是在原始字符串strValue上执行替换操作,因此在循环之后,仅最后一个可移动单词将被删除(示例中甚至没有包含)。 You should store the result of strings.Replace() (you do that), and use this in the next iteration: 您应该存储strings.Replace()的结果(这样做),并在下一次迭代中使用它:

removalString := strValue
for _, wordToRemove := range stringRemove {
    removalString = strings.Replace(removalString, wordToRemove, "", -1)
}

And also use this in your last replacement: 并在最后一次替换中使用它:

result := strings.Replace(removalString, " ", "", -1)
result1 := strings.ToLower(result)

Then output will be (try it on the Go Playground ): 然后输出将是(在Go Playground上尝试):

This is  string
thisisstring

Also note that to remove spaces, you can add that to the list of removable words, and you don't need to always create new variables, you can reuse existing ones. 还要注意,要删除空格,可以将其添加到可移动单词的列表中,并且不必总是创建新变量,可以重复使用现有变量。

This will also perform the same transformation: 这还将执行相同的转换:

s := "This is a string"
words := []string{"a", "an", " "}

for _, word := range words {
    s = strings.Replace(s, word, "", -1)
}

s = strings.ToLower(s)
fmt.Println(s)

Try it on the Go Playground . Go Playground上尝试一下。

this is what youre looking for: 这是您要寻找的:

package main

import (
  "fmt"
  "strings"
)

func main() {
  strValue := "This is a string"
  stringRemove := []string{"a", "an"}
  removalString := strValue
  for _, wordToRemove := range stringRemove {
      removalString = strings.Replace(removalString, wordToRemove, "", -1)
  }
  fmt.Println(removalString)
  result := strings.Replace(strValue, " ", "", -1)
  result1 := strings.ToLower(result)
  fmt.Println(result1)
}

removalString will be set new value each loop. removalString将在每个循环中设置新值。 So fmt.Println(removalString) will show the result of last loop. 因此, fmt.Println(removalString)将显示最后一个循环的结果。

var removalString string
for _, wordToRemove := range stringRemove {
    removalString = strings.Replace(strValue, wordToRemove, "", -1)
}
fmt.Println(removalString)

You may do like this 你可能会这样

strValue := "This is a string"
stringRemove := []string{"a", "an"}
for _, wordToRemove := range stringRemove {
    strValue = strings.Replace(strValue, wordToRemove, "", -1)
}
fmt.Println(strValue)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM