codecamp

GoFrame gstr-查找替换

Replace

  • 说明:​Replace​返回​origin​字符串中, ​search​被​replace​替换后的新字符串。​search​区分大小写。
  • 格式:
Replace(origin, search, replace string, count ...int) string
  • 示例:
func ExampleReplace() {
	var (
		origin  = `golang is very nice!`
		search  = `golang`
		replace = `goframe`
		result  = gstr.Replace(origin, search, replace)
	)
	fmt.Println(result)

	// Output:
	// goframe is very nice!
}

ReplaceI

  • 说明:​ReplaceI​返回​origin​字符串中, ​search​被​replace​替换后的新字符串。​search​不区分大小写。
  • 格式:
ReplaceI(origin, search, replace string, count ...int) string
  • 示例:
func ExampleReplaceI() {
	var (
		origin  = `golang is very nice!`
		search  = `GOLANG`
		replace = `goframe`
		result  = gstr.ReplaceI(origin, search, replace)
	)
	fmt.Println(result)

	// Output:
	// goframe is very nice!
}

ReplaceByArray

  • 说明:​ReplaceByArray​返回​origin​被一个切片按两个一组​(search, replace)​顺序替换的新字符串,区分大小写。  
  • 格式:
ReplaceByArray(origin string, array []string) string
  • 示例:
func ExampleReplaceByArray() {
	{
		var (
			origin = `golang is very nice`
			array  = []string{"lang", "frame"}
			result = gstr.ReplaceByArray(origin, array)
		)
		fmt.Println(result)
	}
	{
		var (
			origin = `golang is very good`
			array  = []string{"golang", "goframe", "good", "nice"}
			result = gstr.ReplaceByArray(origin, array)
		)
		fmt.Println(result)
	}

	// Output:
	// goframe is very nice
	// goframe is very nice
}

ReplaceIByArray

  • 说明:​ReplaceIByArray​返回​origin​被一个切片按两个一组​(search, replace)​顺序替换的新字符串,不区分大小写。  
  • 格式:

ReplaceIByArray(origin string, array []string) string

  • 示例:

func ExampleReplaceIByArray() {
	var (
		origin = `golang is very Good`
		array  = []string{"Golang", "goframe", "GOOD", "nice"}
		result = gstr.ReplaceIByArray(origin, array)
	)

	fmt.Println(result)

	// Output:
	// goframe is very nice
}

ReplaceByMap

  • 说明:​ReplaceByMap​返回​origin​中​map​的​key​替换为​value​的新字符串,区分大小写。  
  • 格式:

ReplaceByMap(origin string, replaces map[string]string) string

  • 示例:

func ExampleReplaceByMap() {
	{
		var (
			origin   = `golang is very nice`
			replaces = map[string]string{
				"lang": "frame",
			}
			result = gstr.ReplaceByMap(origin, replaces)
		)
		fmt.Println(result)
	}
	{
		var (
			origin   = `golang is very good`
			replaces = map[string]string{
				"golang": "goframe",
				"good":   "nice",
			}
			result = gstr.ReplaceByMap(origin, replaces)
		)
		fmt.Println(result)
	}

	// Output:
	// goframe is very nice
	// goframe is very nice
}

ReplaceIByMap

  • 说明:​ReplaceIByMap​返回​origin​中​map​的​key​替换为​value​的新字符串,不区分大小写。  
  • 格式:

ReplaceIByMap(origin string, replaces map[string]string) string

  • 示例:

func ExampleReplaceIByMap() {
	var (
		origin   = `golang is very nice`
		replaces = map[string]string{
			"Lang": "frame",
		}
		result = gstr.ReplaceIByMap(origin, replaces)
	)
	fmt.Println(result)

	// Output:
	// goframe is very nice
}


GoFrame gstr-位置查找
GoFrame gstr-子串截取
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

GoFrame 核心组件

GoFrame 核心组件-数据库ORM

GoFrame 模块列表

GoFrame 模块列表-单元测试

GoFrame 模块列表-功能调试

GoFrame WEB服务开发

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }