英文:
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 (
"fmt"
"strings"
)
// 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 <= 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)
}
Output:
"optimismo from optimism"
"optimismo from "
答案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 := "optimism"
mystring := "optimismo from optimism"
// 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("doesn't exist")
return // error case
}
ind += len(search)
excludingSecond := mystring[:ind]
// run replace on everything after the first one
excludingSecond += strings.Replace(mystring[ind:], search, "", 1)
fmt.Println(excludingSecond)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论