Skip to content

Instantly share code, notes, and snippets.

@armanokka
Created October 28, 2022 20:59
Show Gist options
  • Save armanokka/4056f45cd8f15c0c3bacde8a1c3c81a8 to your computer and use it in GitHub Desktop.
Save armanokka/4056f45cd8f15c0c3bacde8a1c3c81a8 to your computer and use it in GitHub Desktop.
strings.Replace with skipping feature. Golang
// https://go.dev/play/p/0CHrCSOkdbD
func replace(s, old, new string, count, skip int) string {
var startIdx int
for i := 0; i < skip; i++ {
idx := strings.Index(s[startIdx:], old)
if idx == -1 {
return s
}
startIdx += idx + len(old)
}
return s[:startIdx] + strings.Replace(s[startIdx:], old, new, count)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment