阅读(2687) (9)

GoFrame gstr-字符/子串过滤

2022-04-11 15:15:46 更新

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
}