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

     1  /*
     2  
     3  Copyright (c) 2022 - 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  //    output := ReplaceAny("foo bar_baz", '-', []rune(` _`)...)
    13  func ReplaceAny(corpus string, replacement rune, replaced ...rune) string {
    14  	characters := []rune(corpus)
    15  	var c rune
    16  	for x := 0; x < len(characters); x++ {
    17  		c = characters[x]
    18  		for _, r := range replaced {
    19  			if c == r {
    20  				characters[x] = replacement
    21  				break
    22  			}
    23  		}
    24  	}
    25  
    26  	return string(characters)
    27  }