github.com/blend/go-sdk@v1.20240719.1/stringutil/replace_any.go (about)

     1  /*
     2  
     3  Copyright (c) 2024 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package stringutil
     9  
    10  // ReplaceAny replaces any runes in the 'replaced' list with a given replacement.
    11  // Example:
    12  //
    13  //	output := ReplaceAny("foo bar_baz", '-', []rune(` _`)...)
    14  func ReplaceAny(corpus string, replacement rune, replaced ...rune) string {
    15  	characters := []rune(corpus)
    16  	var c rune
    17  	for x := 0; x < len(characters); x++ {
    18  		c = characters[x]
    19  		for _, r := range replaced {
    20  			if c == r {
    21  				characters[x] = replacement
    22  				break
    23  			}
    24  		}
    25  	}
    26  
    27  	return string(characters)
    28  }