github.com/gogf/gf/v2@v2.7.4/text/gstr/gstr_replace.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package gstr
     8  
     9  import (
    10  	"strings"
    11  
    12  	"github.com/gogf/gf/v2/internal/utils"
    13  )
    14  
    15  // Replace returns a copy of the string `origin`
    16  // in which string `search` replaced by `replace` case-sensitively.
    17  func Replace(origin, search, replace string, count ...int) string {
    18  	n := -1
    19  	if len(count) > 0 {
    20  		n = count[0]
    21  	}
    22  	return strings.Replace(origin, search, replace, n)
    23  }
    24  
    25  // ReplaceI returns a copy of the string `origin`
    26  // in which string `search` replaced by `replace` case-insensitively.
    27  func ReplaceI(origin, search, replace string, count ...int) string {
    28  	n := -1
    29  	if len(count) > 0 {
    30  		n = count[0]
    31  	}
    32  	if n == 0 {
    33  		return origin
    34  	}
    35  	var (
    36  		searchLength  = len(search)
    37  		replaceLength = len(replace)
    38  		searchLower   = strings.ToLower(search)
    39  		originLower   string
    40  		pos           int
    41  	)
    42  	for {
    43  		originLower = strings.ToLower(origin)
    44  		if pos = Pos(originLower, searchLower, pos); pos != -1 {
    45  			origin = origin[:pos] + replace + origin[pos+searchLength:]
    46  			pos += replaceLength
    47  			if n--; n == 0 {
    48  				break
    49  			}
    50  		} else {
    51  			break
    52  		}
    53  	}
    54  	return origin
    55  }
    56  
    57  // ReplaceByArray returns a copy of `origin`,
    58  // which is replaced by a slice in order, case-sensitively.
    59  func ReplaceByArray(origin string, array []string) string {
    60  	for i := 0; i < len(array); i += 2 {
    61  		if i+1 >= len(array) {
    62  			break
    63  		}
    64  		origin = Replace(origin, array[i], array[i+1])
    65  	}
    66  	return origin
    67  }
    68  
    69  // ReplaceIByArray returns a copy of `origin`,
    70  // which is replaced by a slice in order, case-insensitively.
    71  func ReplaceIByArray(origin string, array []string) string {
    72  	for i := 0; i < len(array); i += 2 {
    73  		if i+1 >= len(array) {
    74  			break
    75  		}
    76  		origin = ReplaceI(origin, array[i], array[i+1])
    77  	}
    78  	return origin
    79  }
    80  
    81  // ReplaceByMap returns a copy of `origin`,
    82  // which is replaced by a map in unordered way, case-sensitively.
    83  func ReplaceByMap(origin string, replaces map[string]string) string {
    84  	return utils.ReplaceByMap(origin, replaces)
    85  }
    86  
    87  // ReplaceIByMap returns a copy of `origin`,
    88  // which is replaced by a map in unordered way, case-insensitively.
    89  func ReplaceIByMap(origin string, replaces map[string]string) string {
    90  	for k, v := range replaces {
    91  		origin = ReplaceI(origin, k, v)
    92  	}
    93  	return origin
    94  }