codecamp

GoFrame gstr-字符/子串过滤

Trim

  • 说明:​Trim​从字符串的开头和结尾剪切空白(或其他字符)。 可选参数​characterMask​指定额外剥离的字符。
  • 格式:

Trim(str string, characterMask ...string) string

  • 示例:

func ExampleTrim() {
	var (
		str           = `*Hello World*`
		characterMask = "*"
		result        = gstr.Trim(str, characterMask)
	)
	fmt.Println(result)

	// Output:
	// Hello World
}

TrimStr

  • 说明:​TrimStr​从字符串的开头和结尾去掉所有​cut​字符串(不会删除开头或结尾的空白)。  
  • 格式:

TrimStr(str string, cut string, count ...int) string

  • 示例:

func ExampleTrimStr() {
	var (
		str    = `Hello World`
		cut    = "World"
		count  = -1
		result = gstr.TrimStr(str, cut, count)
	)
	fmt.Println(result)

	// Output:
	// Hello
}

TrimLeft

  • 说明:​TrimLeft​将字符串开头的空格(或其他字符)删除。  
  • 格式:

TrimLeft(str string, characterMask ...string) string

  • 示例:

func ExampleTrimLeft() {
	var (
		str           = `*Hello World*`
		characterMask = "*"
		result        = gstr.TrimLeft(str, characterMask)
	)
	fmt.Println(result)

	// Output:
	// Hello World*
}

TrimLeftStr

  • 说明:​TrimLeftStr​从字符串的开头删除​count​个​cut​字符串(不会删除开头的空格)。  
  • 格式:

TrimLeftStr(str string, cut string, count ...int) string

  • 示例:

func ExampleTrimLeftStr() {
	var (
		str    = `**Hello World**`
		cut    = "*"
		count  = 1
		result = gstr.TrimLeftStr(str, cut, count)
	)
	fmt.Println(result)

	// Output:
	// *Hello World**
}

TrimRight

  • 说明:​TrimRight​从字符串的末尾去掉空白(或其他字符)。  
  • 格式:

TrimRight(str string, characterMask ...string) string

  • 示例:

func ExampleTrimRight() {
	var (
		str           = `**Hello World**`
		characterMask = "*def" // []byte{"*", "d", "e", "f"}
		result        = gstr.TrimRight(str, characterMask)
	)
	fmt.Println(result)

	// Output:
	// **Hello Worl
}

TrimRightStr

  • 说明:​TrimRightStr​从字符串的尾部删除​count​个​cut​字符串(不会删除尾部的空格)。  
  • 格式:

TrimRightStr(str string, cut string, count ...int) string

  • 示例:

func ExampleTrimRightStr() {
	var (
		str    = `Hello World!`
		cut    = "!"
		count  = -1
		result = gstr.TrimRightStr(str, cut, count)
	)
	fmt.Println(result)

	// Output:
	// Hello World
}

TrimAll

  • 说明:​TrimAll​删除字符串​str​中的所有空格(或其他字符)以及​characterMask​字符。
  • 格式:

TrimAll(str string, characterMask ...string) string

  • 示例:

func ExampleTrimAll() {
	var (
		str           = `*Hello World*`
		characterMask = "*"
		result        = gstr.TrimAll(str, characterMask)
	)
	fmt.Println(result)

	// Output:
	// HelloWorld
}

HasPrefix

  • 说明:​HasPrefix​返回​s​是否以​prefix​开头。
  • 格式:

HasPrefix(s, prefix string) bool

  • 示例:

func ExampleHasPrefix() {
	var (
		s      = `Hello World`
		prefix = "Hello"
		result = gstr.HasPrefix(s, prefix)
	)
	fmt.Println(result)

	// Output:
	// true
}

HasSuffix

  • 说明:​HasSuffix​返回​s​是否以​suffix ​结束。
  • 格式:

HasSuffix(s, suffix string) bool

  • 示例:

func ExampleHasSuffix() {
	var (
		s      = `my best love is goframe`
		prefix = "goframe"
		result = gstr.HasSuffix(s, prefix)
	)
	fmt.Println(result)

	// Output:
	// true
}


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; }