10 examples of 'golang string replace' in Go

Every line of 'golang string replace' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
29func (v Vars) Replace(s string) string {
30 for k, v := range v {
31 prefix := regexp.MustCompile(fmt.Sprintf("{%s=([^}]*)}", k))
32 if v != "" {
33 s = prefix.ReplaceAllString(s, "$1")
34 } else {
35 s = prefix.ReplaceAllString(s, "")
36 }
37 s = strings.Replace(s, fmt.Sprintf("{%s}", k), v, -1)
38 }
39 return s
40}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

194func (ns *Namespace) Replace(s, old, new interface{}, limit ...interface{}) (string, error) {
195 ss, err := cast.ToStringE(s)
196 if err != nil {
197 return "", err
198 }
199
200 so, err := cast.ToStringE(old)
201 if err != nil {
202 return "", err
203 }
204
205 sn, err := cast.ToStringE(new)
206 if err != nil {
207 return "", err
208 }
209
210 if len(limit) == 0 {
211 return strings.ReplaceAll(ss, so, sn), nil
212 }
213
214 lim, err := cast.ToIntE(limit[0])
215 if err != nil {
216 return "", err
217 }
218
219 return strings.Replace(ss, so, sn, lim), nil
220}
150func replace(s, old, new string) string {
151 return strings.Replace(s, old, new, -1)
152}
113func replace(old, new, src string) string {
114 return strings.Replace(src, old, new, -1)
115}
273func (r *genericReplacer) Replace(s string) string {
274 buf := make(appendSliceWriter, 0, len(s))
275 r.WriteString(&buf, s)
276 return string(buf)
277}
33func (o MTPLContext) Replace(sql string, args []interface{}) (r string) {
34 if strings.EqualFold(sql, "") || args == nil {
35 return sql
36 }
37 word, _ := regexp.Compile(fmt.Sprintf(`\%s([,|\ ;)]|$)`, o.prefix))
38 index := -1
39 sql = word.ReplaceAllStringFunc(sql, func(s string) string {
40 index++
41 if index >= len(args) {
42 return "NULL" + s[1:]
43 }
44 return fmt.Sprintf("'%v'%s", args[index], s[1:])
45 })
46 return sql
47}
88func ReplaceString(pattern, replace, src string) (string, error) {
89 r, e := Replace(pattern, []byte(replace), []byte(src))
90 return string(r), e
91}
198func (re *Regexp) ReplaceAllLiteralString(src, repl string) string {
199 if re.Regexp == nil {
200 return ""
201 }
202 return replaceAllLiteralStringCache.do(re.Regexp, src, repl, re.Regexp.ReplaceAllLiteralString)
203}
100func (params *replaceParams) doReplace() string {
101 return strings.Replace(params.subject, params.search.(string), params.replace.(string), params.count)
102}
22func ReplaceAll(str string, expr string, replaceStr string) string {
23 reg := regexp.MustCompile(expr)
24 str = reg.ReplaceAllString(str, replaceStr)
25 return str
26}

Related snippets