Created
October 28, 2022 20:59
-
-
Save armanokka/4056f45cd8f15c0c3bacde8a1c3c81a8 to your computer and use it in GitHub Desktop.
strings.Replace with skipping feature. Golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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