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

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

Replace Nth occurrence of string in golang

问题

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

package main

import (
	"fmt"
	"strings"
)

func main() {
	mystring := "optimismo from optimism"
	excludingSecond := strings.Replace(mystring, "optimism", "", 1)
	fmt.Println(excludingSecond)
}

你可以使用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

package main

import (
	"fmt"
	"strings"
)

func main() {
	mystring := "optimismo from optimism"
	excludingSecond := strings.Replace(mystring, "optimism", "", 1)
	fmt.Println(excludingSecond)
}

答案1

得分: 11

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

package main

import (
    "fmt"
    "strings"
)

func main() {
    mystring := "optimismo from optimism"

    i := strings.LastIndex(mystring, "optimism")
    excludingLast := mystring[:i] + strings.Replace(mystring[i:], "optimism", "", 1)
    fmt.Println(excludingLast)
}

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

英文:

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

package main

import (
    "fmt"
    "strings"
)

func main() {
    mystring := "optimismo from optimism"

    i := strings.LastIndex(mystring, "optimism")
    excludingLast := mystring[:i] + strings.Replace(mystring[i:], "optimism", "", 1)
    fmt.Println(excludingLast)
}

答案2

得分: 7

例如,

package main

import (
	"fmt"
	"strings"
)

// 用新的字符串替换s中第n个出现的old字符串。
func replaceNth(s, old, new string, n int) string {
	i := 0
	for m := 1; m <= n; m++ {
		x := strings.Index(s[i:], old)
		if x < 0 {
			break
		}
		i += x
		if m == n {
			return s[:i] + new + s[i+len(old):]
		}
		i += len(old)
	}
	return s
}

func main() {
	s := "optimismo from optimism"
	fmt.Printf("%q\n", s)
	t := replaceNth(s, "optimism", "", 2)
	fmt.Printf("%q\n", t)
}

输出:

"optimismo from optimism"
"optimismo from "
英文:

For example,

package main

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

// Replace the nth occurrence of old in s by new.
func replaceNth(s, old, new string, n int) string {
	i := 0
	for m := 1; m &lt;= n; m++ {
		x := strings.Index(s[i:], old)
		if x &lt; 0 {
			break
		}
		i += x
		if m == n {
			return s[:i] + new + s[i+len(old):]
		}
		i += len(old)
	}
	return s
}

func main() {
	s := &quot;optimismo from optimism&quot;
	fmt.Printf(&quot;%q\n&quot;, s)
	t := replaceNth(s, &quot;optimism&quot;, &quot;&quot;, 2)
	fmt.Printf(&quot;%q\n&quot;, t)
}

Output:

&quot;optimismo from optimism&quot;
&quot;optimismo from &quot;

答案3

得分: 3

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

func main() {
    search := "optimism"
    mystring := "optimismo from optimism"
    
    // 找到第一个的索引并加上长度以获取单词的结尾
    ind := strings.Index(mystring, search)
    if ind == -1 {
        fmt.Println("不存在")
        return // 错误情况
    }
    ind += len(search)

    excludingSecond := mystring[:ind]
    
    // 在第一个之后的所有内容上运行替换
    excludingSecond += strings.Replace(mystring[ind:], search, "", 1)
    fmt.Println(excludingSecond)
}

希望对你有帮助!

英文:

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

func main() {
	search := &quot;optimism&quot;
	mystring := &quot;optimismo from optimism&quot;
	
	// find index of the first and add the length to get the end of the word
	ind := strings.Index(mystring, search)
	if ind == -1 {
		fmt.Println(&quot;doesn&#39;t exist&quot;)
		return // error case
	}
	ind += len(search)

	excludingSecond := mystring[:ind]
	
	// run replace on everything after the first one
	excludingSecond += strings.Replace(mystring[ind:], search, &quot;&quot;, 1)
	fmt.Println(excludingSecond)
}

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:

确定