在golang中替换第N个出现的字符串

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

Replace Nth occurrence of string in golang

问题

如何在golang中替换第n个(在这种情况下是第二个)字符串出现?以下代码将示例字符串optimismo from optimism替换为o from optimism,而我希望它是optimismo from

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. func main() {
  7. mystring := "optimismo from optimism"
  8. excludingSecond := strings.Replace(mystring, "optimism", "", 1)
  9. fmt.Println(excludingSecond)
  10. }

你可以使用strings.Replace函数来替换字符串。该函数的第三个参数是要替换的字符串,第四个参数是替换的次数。在你的代码中,你将optimism替换为空字符串,并指定替换一次。如果你想替换第二次出现的字符串,可以将第四个参数设置为2。

英文:

How do I replace the nth (in this case second) occurrence of a string in golang? The following code replaces the example string optimismo from optimism with o from optimism when I want it to be optimismo from

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. func main() {
  7. mystring := "optimismo from optimism"
  8. excludingSecond := strings.Replace(mystring, "optimism", "", 1)
  9. fmt.Println(excludingSecond)
  10. }

答案1

得分: 11

对于任何在这篇帖子上遇到问题并且想要替换最后一次出现的人

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. func main() {
  7. mystring := "optimismo from optimism"
  8. i := strings.LastIndex(mystring, "optimism")
  9. excludingLast := mystring[:i] + strings.Replace(mystring[i:], "optimism", "", 1)
  10. fmt.Println(excludingLast)
  11. }

如果有任何问题,请告诉我。

英文:

For anyone stumbling on this post and is looking to replace the LAST occurrence

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. func main() {
  7. mystring := "optimismo from optimism"
  8. i := strings.LastIndex(mystring, "optimism")
  9. excludingLast := mystring[:i] + strings.Replace(mystring[i:], "optimism", "", 1)
  10. fmt.Println(excludingLast)
  11. }

答案2

得分: 7

例如,

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. // 用新的字符串替换s中第n个出现的old字符串。
  7. func replaceNth(s, old, new string, n int) string {
  8. i := 0
  9. for m := 1; m <= n; m++ {
  10. x := strings.Index(s[i:], old)
  11. if x < 0 {
  12. break
  13. }
  14. i += x
  15. if m == n {
  16. return s[:i] + new + s[i+len(old):]
  17. }
  18. i += len(old)
  19. }
  20. return s
  21. }
  22. func main() {
  23. s := "optimismo from optimism"
  24. fmt.Printf("%q\n", s)
  25. t := replaceNth(s, "optimism", "", 2)
  26. fmt.Printf("%q\n", t)
  27. }

输出:

  1. "optimismo from optimism"
  2. "optimismo from "
英文:

For example,

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;strings&quot;
  5. )
  6. // Replace the nth occurrence of old in s by new.
  7. func replaceNth(s, old, new string, n int) string {
  8. i := 0
  9. for m := 1; m &lt;= n; m++ {
  10. x := strings.Index(s[i:], old)
  11. if x &lt; 0 {
  12. break
  13. }
  14. i += x
  15. if m == n {
  16. return s[:i] + new + s[i+len(old):]
  17. }
  18. i += len(old)
  19. }
  20. return s
  21. }
  22. func main() {
  23. s := &quot;optimismo from optimism&quot;
  24. fmt.Printf(&quot;%q\n&quot;, s)
  25. t := replaceNth(s, &quot;optimism&quot;, &quot;&quot;, 2)
  26. fmt.Printf(&quot;%q\n&quot;, t)
  27. }

Output:

  1. &quot;optimismo from optimism&quot;
  2. &quot;optimismo from &quot;

答案3

得分: 3

如果你总是知道会有两个,你可以使用https://godoc.org/strings#Index来找到第一个的索引,然后在之后的所有内容上进行替换,最后将它们组合在一起。

  1. func main() {
  2. search := "optimism"
  3. mystring := "optimismo from optimism"
  4. // 找到第一个的索引并加上长度以获取单词的结尾
  5. ind := strings.Index(mystring, search)
  6. if ind == -1 {
  7. fmt.Println("不存在")
  8. return // 错误情况
  9. }
  10. ind += len(search)
  11. excludingSecond := mystring[:ind]
  12. // 在第一个之后的所有内容上运行替换
  13. excludingSecond += strings.Replace(mystring[ind:], search, "", 1)
  14. fmt.Println(excludingSecond)
  15. }

希望对你有帮助!

英文:

If you always know there will be two, you could use https://godoc.org/strings#Index to find the index of the first, then do the replace on everything after, finally combining them together.

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

  1. func main() {
  2. search := &quot;optimism&quot;
  3. mystring := &quot;optimismo from optimism&quot;
  4. // find index of the first and add the length to get the end of the word
  5. ind := strings.Index(mystring, search)
  6. if ind == -1 {
  7. fmt.Println(&quot;doesn&#39;t exist&quot;)
  8. return // error case
  9. }
  10. ind += len(search)
  11. excludingSecond := mystring[:ind]
  12. // run replace on everything after the first one
  13. excludingSecond += strings.Replace(mystring[ind:], search, &quot;&quot;, 1)
  14. fmt.Println(excludingSecond)
  15. }

huangapple
  • 本文由 发表于 2017年5月24日 04:43:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/44144641.html
匿名

发表评论

匿名网友

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

确定