gstr

package
v2.7.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 9, 2024 License: MIT Imports: 14 Imported by: 492

Documentation

Overview

Package gstr provides functions for string handling.

Index

Examples

Constants

View Source
const (
	// NotFoundIndex is the position index for string not found in searching functions.
	NotFoundIndex = -1
)

Variables

This section is empty.

Functions

func AddSlashes

func AddSlashes(str string) string

AddSlashes quotes with slashes `\` for chars: '"\.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		str    = `'aa'"bb"cc\r\n\d\t`
		result = gstr.AddSlashes(str)
	)

	fmt.Println(result)

}
Output:

\'aa\'\"bb\"cc\\r\\n\\d\\t

func CaseCamel

func CaseCamel(s string) string

CaseCamel converts a string to CamelCase.

Example: CaseCamel("any_kind_of_string") -> AnyKindOfString

Example

case

package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		str    = `hello world`
		result = gstr.CaseCamel(str)
	)
	fmt.Println(result)

}
Output:

HelloWorld

func CaseCamelLower

func CaseCamelLower(s string) string

CaseCamelLower converts a string to lowerCamelCase.

Example: CaseCamelLower("any_kind_of_string") -> anyKindOfString

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		str    = `hello world`
		result = gstr.CaseCamelLower(str)
	)
	fmt.Println(result)

}
Output:

helloWorld

func CaseConvert added in v2.5.7

func CaseConvert(s string, caseType CaseType) string

CaseConvert converts a string to the specified naming convention. Use CaseTypeMatch to match the case type from string.

func CaseDelimited

func CaseDelimited(s string, del byte) string

CaseDelimited converts a string to snake.case.delimited.

Example: CaseDelimited("AnyKindOfString", '.') -> any.kind.of.string

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		str    = `hello world`
		del    = byte('-')
		result = gstr.CaseDelimited(str, del)
	)
	fmt.Println(result)

}
Output:

hello-world

func CaseDelimitedScreaming

func CaseDelimitedScreaming(s string, del uint8, screaming bool) string

CaseDelimitedScreaming converts a string to DELIMITED.SCREAMING.CASE or delimited.screaming.case.

Example: CaseDelimitedScreaming("AnyKindOfString", '.') -> ANY.KIND.OF.STRING

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	{
		var (
			str    = `hello world`
			del    = byte('-')
			result = gstr.CaseDelimitedScreaming(str, del, true)
		)
		fmt.Println(result)
	}
	{
		var (
			str    = `hello world`
			del    = byte('-')
			result = gstr.CaseDelimitedScreaming(str, del, false)
		)
		fmt.Println(result)
	}

}
Output:

HELLO-WORLD
hello-world

func CaseKebab

func CaseKebab(s string) string

CaseKebab converts a string to kebab-case.

Example: CaseKebab("AnyKindOfString") -> any-kind-of-string

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		str    = `hello world`
		result = gstr.CaseKebab(str)
	)
	fmt.Println(result)

}
Output:

hello-world

func CaseKebabScreaming

func CaseKebabScreaming(s string) string

CaseKebabScreaming converts a string to KEBAB-CASE-SCREAMING.

Example: CaseKebab("AnyKindOfString") -> ANY-KIND-OF-STRING

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		str    = `hello world`
		result = gstr.CaseKebabScreaming(str)
	)
	fmt.Println(result)

}
Output:

HELLO-WORLD

func CaseSnake

func CaseSnake(s string) string

CaseSnake converts a string to snake_case.

Example: CaseSnake("AnyKindOfString") -> any_kind_of_string

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		str    = `hello world`
		result = gstr.CaseSnake(str)
	)
	fmt.Println(result)

}
Output:

hello_world

func CaseSnakeFirstUpper

func CaseSnakeFirstUpper(word string, underscore ...string) string

CaseSnakeFirstUpper converts a string like "RGBCodeMd5" to "rgb_code_md5". TODO for efficiency should change regexp to traversing string in future.

Example: CaseSnakeFirstUpper("RGBCodeMd5") -> rgb_code_md5

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		str    = `RGBCodeMd5`
		result = gstr.CaseSnakeFirstUpper(str)
	)
	fmt.Println(result)

}
Output:

rgb_code_md5

func CaseSnakeScreaming

func CaseSnakeScreaming(s string) string

CaseSnakeScreaming converts a string to SNAKE_CASE_SCREAMING.

Example: CaseSnakeScreaming("AnyKindOfString") -> ANY_KIND_OF_STRING

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		str    = `hello world`
		result = gstr.CaseSnakeScreaming(str)
	)
	fmt.Println(result)

}
Output:

HELLO_WORLD

func Chr

func Chr(ascii int) string

Chr return the ascii string of a number(0-255).

Example: Chr(65) -> "A"

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		ascii  = 65 // A
		result = gstr.Chr(ascii)
	)
	fmt.Println(result)

}
Output:

A

func ChunkSplit

func ChunkSplit(body string, chunkLen int, end string) string

ChunkSplit splits a string into smaller chunks. Can be used to split a string into smaller chunks which is useful for e.g. converting BASE64 string output to match RFC 2045 semantics. It inserts end every chunkLen characters. It considers parameter `body` and `end` as unicode string.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		body     = `1234567890`
		chunkLen = 2
		end      = "#"
		result   = gstr.ChunkSplit(body, chunkLen, end)
	)
	fmt.Println(result)

}
Output:

12#34#56#78#90#

func Compare

func Compare(a, b string) int

Compare returns an integer comparing two strings lexicographically. The result will be 0 if a==b, -1 if a < b, and +1 if a > b.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	fmt.Println(gstr.Compare("c", "c"))
	fmt.Println(gstr.Compare("a", "b"))
	fmt.Println(gstr.Compare("c", "b"))

}
Output:

0
-1
1

func CompareVersion

func CompareVersion(a, b string) int

CompareVersion compares `a` and `b` as standard GNU version.

It returns 1 if `a` > `b`.

It returns -1 if `a` < `b`.

It returns 0 if `a` = `b`.

GNU standard version is like: v1.0 1 1.0.0 v1.0.1 v2.10.8 10.2.0 etc.

Example

version

package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	fmt.Println(gstr.CompareVersion("v2.11.9", "v2.10.8"))
	fmt.Println(gstr.CompareVersion("1.10.8", "1.19.7"))
	fmt.Println(gstr.CompareVersion("2.8.beta", "2.8"))

}
Output:

1
-1
0

func CompareVersionGo

func CompareVersionGo(a, b string) int

CompareVersionGo compares `a` and `b` as standard Golang version.

It returns 1 if `a` > `b`.

It returns -1 if `a` < `b`.

It returns 0 if `a` = `b`.

Golang standard version is like: 1.0.0 v1.0.1 v2.10.8 10.2.0 v0.0.0-20190626092158-b2ccc519800e v1.12.2-0.20200413154443-b17e3a6804fa v4.20.0+incompatible etc.

Docs: https://go.dev/doc/modules/version-numbers

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	fmt.Println(gstr.CompareVersionGo("v2.11.9", "v2.10.8"))
	fmt.Println(gstr.CompareVersionGo("v4.20.1", "v4.20.1+incompatible"))
	fmt.Println(gstr.CompareVersionGo(
		"v0.0.2-20180626092158-b2ccc119800e",
		"v1.0.1-20190626092158-b2ccc519800e",
	))

}
Output:

1
1
-1

func Contains

func Contains(str, substr string) bool

Contains reports whether `substr` is within `str`, case-sensitively.

Example

contain

package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	{
		var (
			str    = `Hello World`
			substr = `Hello`
			result = gstr.Contains(str, substr)
		)
		fmt.Println(result)
	}
	{
		var (
			str    = `Hello World`
			substr = `hello`
			result = gstr.Contains(str, substr)
		)
		fmt.Println(result)
	}

}
Output:

true
false

func ContainsAny

func ContainsAny(s, chars string) bool

ContainsAny reports whether any Unicode code points in `chars` are within `s`.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	{
		var (
			s      = `goframe`
			chars  = "g"
			result = gstr.ContainsAny(s, chars)
		)
		fmt.Println(result)
	}
	{
		var (
			s      = `goframe`
			chars  = "G"
			result = gstr.ContainsAny(s, chars)
		)
		fmt.Println(result)
	}

}
Output:

true
false

func ContainsI

func ContainsI(str, substr string) bool

ContainsI reports whether substr is within str, case-insensitively.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		str     = `Hello World`
		substr  = "hello"
		result1 = gstr.Contains(str, substr)
		result2 = gstr.ContainsI(str, substr)
	)
	fmt.Println(result1)
	fmt.Println(result2)

}
Output:

false
true

func Count

func Count(s, substr string) int

Count counts the number of `substr` appears in `s`. It returns 0 if no `substr` found in `s`.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		str     = `goframe is very, very easy to use`
		substr1 = "goframe"
		substr2 = "very"
		result1 = gstr.Count(str, substr1)
		result2 = gstr.Count(str, substr2)
	)
	fmt.Println(result1)
	fmt.Println(result2)

}
Output:

1
2

func CountChars

func CountChars(str string, noSpace ...bool) map[string]int

CountChars returns information about chars' count used in a string. It considers parameter `str` as unicode string.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		str    = `goframe`
		result = gstr.CountChars(str)
	)
	fmt.Println(result)

	// May Output:
	// map[a:1 e:1 f:1 g:1 m:1 o:1 r:1]
}
Output:

func CountI

func CountI(s, substr string) int

CountI counts the number of `substr` appears in `s`, case-insensitively. It returns 0 if no `substr` found in `s`.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		str     = `goframe is very, very easy to use`
		substr1 = "GOFRAME"
		substr2 = "VERY"
		result1 = gstr.CountI(str, substr1)
		result2 = gstr.CountI(str, substr2)
	)
	fmt.Println(result1)
	fmt.Println(result2)

}
Output:

1
2

func CountWords

func CountWords(str string) map[string]int

CountWords returns information about words' count used in a string. It considers parameter `str` as unicode string.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		str    = `goframe is very, very easy to use!`
		result = gstr.CountWords(str)
	)
	fmt.Printf(`%#v`, result)

}
Output:

map[string]int{"easy":1, "goframe":1, "is":1, "to":1, "use!":1, "very":1, "very,":1}

func Equal

func Equal(a, b string) bool

Equal reports whether `a` and `b`, interpreted as UTF-8 strings, are equal under Unicode case-folding, case-insensitively.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	fmt.Println(gstr.Equal(`A`, `a`))
	fmt.Println(gstr.Equal(`A`, `A`))
	fmt.Println(gstr.Equal(`A`, `B`))

}
Output:

true
true
false

func Explode

func Explode(delimiter, str string) []string

Explode splits string `str` by a string `delimiter`, to an array. See http://php.net/manual/en/function.explode.php.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		str       = `Hello World`
		delimiter = " "
		result    = gstr.Explode(delimiter, str)
	)
	fmt.Printf(`%#v`, result)

}
Output:

[]string{"Hello", "World"}

func Fields

func Fields(str string) []string

Fields returns the words used in a string as slice.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		str    = `Hello World`
		result = gstr.Fields(str)
	)
	fmt.Printf(`%#v`, result)

}
Output:

[]string{"Hello", "World"}

func HasPrefix

func HasPrefix(s, prefix string) bool

HasPrefix tests whether the string s begins with prefix.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

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

}
Output:

true

func HasSuffix

func HasSuffix(s, suffix string) bool

HasSuffix tests whether the string s ends with suffix.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

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

}
Output:

true

func HideStr

func HideStr(str string, percent int, hide string) string

HideStr replaces part of the string `str` to `hide` by `percentage` from the `middle`. It considers parameter `str` as unicode string.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		str     = `13800138000`
		percent = 40
		hide    = `*`
		result  = gstr.HideStr(str, percent, hide)
	)
	fmt.Println(result)

}
Output:

138****8000

func Implode

func Implode(glue string, pieces []string) string

Implode joins array elements `pieces` with a string `glue`. http://php.net/manual/en/function.implode.php

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		pieces = []string{"goframe", "is", "very", "easy", "to", "use"}
		glue   = " "
		result = gstr.Implode(glue, pieces)
	)
	fmt.Println(result)

}
Output:

goframe is very easy to use

func InArray

func InArray(a []string, s string) bool

InArray checks whether string `s` in slice `a`.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		a      = []string{"goframe", "is", "very", "easy", "to", "use"}
		s      = "goframe"
		result = gstr.InArray(a, s)
	)
	fmt.Println(result)

}
Output:

true

func IsGNUVersion added in v2.1.2

func IsGNUVersion(version string) bool

IsGNUVersion checks and returns whether given `version` is valid GNU version string.

func IsLetterLower

func IsLetterLower(b byte) bool

IsLetterLower tests whether the given byte b is in lower case.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	fmt.Println(gstr.IsLetterLower('a'))
	fmt.Println(gstr.IsLetterLower('A'))

}
Output:

true
false

func IsLetterUpper

func IsLetterUpper(b byte) bool

IsLetterUpper tests whether the given byte b is in upper case.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	fmt.Println(gstr.IsLetterUpper('A'))
	fmt.Println(gstr.IsLetterUpper('a'))

}
Output:

true
false

func IsNumeric

func IsNumeric(s string) bool

IsNumeric tests whether the given string s is numeric.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	fmt.Println(gstr.IsNumeric("88"))
	fmt.Println(gstr.IsNumeric("3.1415926"))
	fmt.Println(gstr.IsNumeric("abc"))
}
Output:

true
true
false

func IsSubDomain

func IsSubDomain(subDomain string, mainDomain string) bool

IsSubDomain checks whether `subDomain` is sub-domain of mainDomain. It supports '*' in `mainDomain`.

Example

domain

package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		subDomain  = `s.goframe.org`
		mainDomain = `goframe.org`
		result     = gstr.IsSubDomain(subDomain, mainDomain)
	)
	fmt.Println(result)

}
Output:

true

func Join

func Join(array []string, sep string) string

Join concatenates the elements of `array` to create a single string. The separator string `sep` is placed between elements in the resulting string.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		array  = []string{"goframe", "is", "very", "easy", "to", "use"}
		sep    = ` `
		result = gstr.Join(array, sep)
	)
	fmt.Println(result)

}
Output:

goframe is very easy to use

func JoinAny

func JoinAny(array interface{}, sep string) string

JoinAny concatenates the elements of `array` to create a single string. The separator string `sep` is placed between elements in the resulting string.

The parameter `array` can be any type of slice, which be converted to string array.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		sep    = `,`
		arr2   = []int{99, 73, 85, 66}
		result = gstr.JoinAny(arr2, sep)
	)
	fmt.Println(result)

}
Output:

99,73,85,66

func LcFirst

func LcFirst(s string) string

LcFirst returns a copy of the string s with the first letter mapped to its lower case.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		str    = `Goframe`
		result = gstr.LcFirst(str)
	)
	fmt.Println(result)

}
Output:

goframe

func LenRune

func LenRune(str string) int

LenRune returns string length of unicode.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		str    = `GoFrame框架`
		result = gstr.LenRune(str)
	)
	fmt.Println(result)

}
Output:

9

func Levenshtein

func Levenshtein(str1, str2 string, costIns, costRep, costDel int) int

Levenshtein calculates Levenshtein distance between two strings. costIns: Defines the cost of insertion. costRep: Defines the cost of replacement. costDel: Defines the cost of deletion. See http://php.net/manual/en/function.levenshtein.php.

Example

levenshtein

package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		str1    = "Hello World"
		str2    = "hallo World"
		costIns = 1
		costRep = 1
		costDel = 1
		result  = gstr.Levenshtein(str1, str2, costIns, costRep, costDel)
	)
	fmt.Println(result)

}
Output:

2

func List2 added in v2.5.5

func List2(str, delimiter string) (part1, part2 string)

List2 Split the `str` with `delimiter` and returns the result as two parts string.

func List3 added in v2.5.5

func List3(str, delimiter string) (part1, part2, part3 string)

List3 Split the `str` with `delimiter` and returns the result as three parts string.

func ListAndTrim2 added in v2.5.5

func ListAndTrim2(str, delimiter string) (part1, part2 string)

ListAndTrim2 SplitAndTrim the `str` with `delimiter` and returns the result as two parts string.

func ListAndTrim3 added in v2.5.5

func ListAndTrim3(str, delimiter string) (part1, part2, part3 string)

ListAndTrim3 SplitAndTrim the `str` with `delimiter` and returns the result as three parts string.

func Nl2Br

func Nl2Br(str string, isXhtml ...bool) string

Nl2Br inserts HTML line breaks(`br`|<br />) before all newlines in a string: \n\r, \r\n, \r, \n. It considers parameter `str` as unicode string.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		str = `goframe
is
very
easy
to
use`
		result = gstr.Nl2Br(str)
	)

	fmt.Println(result)

}
Output:

goframe<br>is<br>very<br>easy<br>to<br>use

func NumberFormat

func NumberFormat(number float64, decimals int, decPoint, thousandsSep string) string

NumberFormat formats a number with grouped thousands. Parameter `decimals`: Sets the number of decimal points. Parameter `decPoint`: Sets the separator for the decimal point. Parameter `thousandsSep`: Sets the thousands' separator. See http://php.net/manual/en/function.number-format.php.

Example: NumberFormat(1234.56, 2, ".", "") -> 1234,56 NumberFormat(1234.56, 2, ",", " ") -> 1 234,56

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		number       float64 = 123456
		decimals             = 2
		decPoint             = "."
		thousandsSep         = ","
		result               = gstr.NumberFormat(number, decimals, decPoint, thousandsSep)
	)
	fmt.Println(result)

}
Output:

123,456.00

func OctStr

func OctStr(str string) string

OctStr converts string container octal string to its original string, for example, to Chinese string.

Example: OctStr("\346\200\241") -> 怡

Example

convert

package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		str    = `\346\200\241`
		result = gstr.OctStr(str)
	)
	fmt.Println(result)

}
Output:

func Ord

func Ord(char string) int

Ord converts the first byte of a string to a value between 0 and 255.

Example: Chr("A") -> 65

Example

'103' is the 'g' in ASCII

package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		str    = `goframe`
		result = gstr.Ord(str)
	)

	fmt.Println(result)

}
Output:

103

func Parse

func Parse(s string) (result map[string]interface{}, err error)

Parse parses the string into map[string]interface{}.

v1=m&v2=n -> map[v1:m v2:n] v[a]=m&v[b]=n -> map[v:map[a:m b:n]] v[a][a]=m&v[a][b]=n -> map[v:map[a:map[a:m b:n]]] v[]=m&v[]=n -> map[v:[m n]] v[a][]=m&v[a][]=n -> map[v:map[a:[m n]]] v[][]=m&v[][]=n -> map[v:[map[]]] // Currently does not support nested slice. v=m&v[a]=n -> error a .[[b=c -> map[a___[b:c]

Example

parse

package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	{
		var (
			str       = `v1=m&v2=n`
			result, _ = gstr.Parse(str)
		)
		fmt.Println(result)
	}
	{
		var (
			str       = `v[a][a]=m&v[a][b]=n`
			result, _ = gstr.Parse(str)
		)
		fmt.Println(result)
	}
	{
		// The form of nested Slice is not yet supported.
		var str = `v[][]=m&v[][]=n`
		result, err := gstr.Parse(str)
		if err != nil {
			panic(err)
		}
		fmt.Println(result)
	}
	{
		// This will produce an error.
		var str = `v=m&v[a]=n`
		result, err := gstr.Parse(str)
		if err != nil {
			println(err)
		}
		fmt.Println(result)
	}
	{
		var (
			str       = `a .[[b=c`
			result, _ = gstr.Parse(str)
		)
		fmt.Println(result)
	}

	// May Output:
	// map[v1:m v2:n]
	// map[v:map[a:map[a:m b:n]]]
	// map[v:map[]]
	// Error: expected type 'map[string]interface{}' for key 'v', but got 'string'
	// map[]
	// map[a___[b:c]
}
Output:

func Pos

func Pos(haystack, needle string, startOffset ...int) int

Pos returns the position of the first occurrence of `needle` in `haystack` from `startOffset`, case-sensitively. It returns -1, if not found.

Example

pos

package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		haystack = `Hello World`
		needle   = `World`
		result   = gstr.Pos(haystack, needle)
	)
	fmt.Println(result)

}
Output:

6

func PosI

func PosI(haystack, needle string, startOffset ...int) int

PosI returns the position of the first occurrence of `needle` in `haystack` from `startOffset`, case-insensitively. It returns -1, if not found.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		haystack = `goframe is very, very easy to use`
		needle   = `very`
		posI     = gstr.PosI(haystack, needle)
		posR     = gstr.PosR(haystack, needle)
	)
	fmt.Println(posI)
	fmt.Println(posR)

}
Output:

11
17

func PosIRune

func PosIRune(haystack, needle string, startOffset ...int) int

PosIRune acts like function PosI but considers `haystack` and `needle` as unicode string.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	{
		var (
			haystack    = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架`
			needle      = `高性能`
			startOffset = 10
			result      = gstr.PosIRune(haystack, needle, startOffset)
		)
		fmt.Println(result)
	}
	{
		var (
			haystack    = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架`
			needle      = `高性能`
			startOffset = 30
			result      = gstr.PosIRune(haystack, needle, startOffset)
		)
		fmt.Println(result)
	}

}
Output:

14
-1

func PosR

func PosR(haystack, needle string, startOffset ...int) int

PosR returns the position of the last occurrence of `needle` in `haystack` from `startOffset`, case-sensitively. It returns -1, if not found.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		haystack = `goframe is very, very easy to use`
		needle   = `very`
		posI     = gstr.PosI(haystack, needle)
		posR     = gstr.PosR(haystack, needle)
	)
	fmt.Println(posI)
	fmt.Println(posR)

}
Output:

11
17

func PosRI

func PosRI(haystack, needle string, startOffset ...int) int

PosRI returns the position of the last occurrence of `needle` in `haystack` from `startOffset`, case-insensitively. It returns -1, if not found.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		haystack = `goframe is very, very easy to use`
		needle   = `VERY`
		posI     = gstr.PosI(haystack, needle)
		posR     = gstr.PosRI(haystack, needle)
	)
	fmt.Println(posI)
	fmt.Println(posR)

}
Output:

11
17

func PosRIRune

func PosRIRune(haystack, needle string, startOffset ...int) int

PosRIRune acts like function PosRI but considers `haystack` and `needle` as unicode string.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		haystack = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架`
		needle   = `GO`
		posI     = gstr.PosIRune(haystack, needle)
		posR     = gstr.PosRIRune(haystack, needle)
	)
	fmt.Println(posI)
	fmt.Println(posR)

}
Output:

0
22

func PosRRune

func PosRRune(haystack, needle string, startOffset ...int) int

PosRRune acts like function PosR but considers `haystack` and `needle` as unicode string.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		haystack = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架`
		needle   = `Go`
		posI     = gstr.PosIRune(haystack, needle)
		posR     = gstr.PosRRune(haystack, needle)
	)
	fmt.Println(posI)
	fmt.Println(posR)

}
Output:

0
22

func PosRune

func PosRune(haystack, needle string, startOffset ...int) int

PosRune acts like function Pos but considers `haystack` and `needle` as unicode string.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		haystack = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架`
		needle   = `Go`
		posI     = gstr.PosRune(haystack, needle)
		posR     = gstr.PosRRune(haystack, needle)
	)
	fmt.Println(posI)
	fmt.Println(posR)

}
Output:

0
22

func PrefixArray

func PrefixArray(array []string, prefix string)

PrefixArray adds `prefix` string for each item of `array`.

Example: PrefixArray(["a","b"], "gf_") -> ["gf_a", "gf_b"]

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		strArray = []string{"tom", "lily", "john"}
	)

	gstr.PrefixArray(strArray, "classA_")

	fmt.Println(strArray)

}
Output:

[classA_tom classA_lily classA_john]

func QuoteMeta

func QuoteMeta(str string, chars ...string) string

QuoteMeta returns a version of `str` with a backslash character (`\`). If custom chars `chars` not given, it uses default chars: .\+*?[^]($)

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	{
		var (
			str    = `.\+?[^]()`
			result = gstr.QuoteMeta(str)
		)
		fmt.Println(result)
	}
	{
		var (
			str    = `https://goframe.org/pages/viewpage.action?pageId=1114327`
			result = gstr.QuoteMeta(str)
		)
		fmt.Println(result)
	}

}
Output:

\.\\\+\?\[\^\]\(\)
https://goframe\.org/pages/viewpage\.action\?pageId=1114327

func Repeat

func Repeat(input string, multiplier int) string

Repeat returns a new string consisting of multiplier copies of the string input.

Example: Repeat("a", 3) -> "aaa"

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		input      = `goframe `
		multiplier = 3
		result     = gstr.Repeat(input, multiplier)
	)
	fmt.Println(result)

}
Output:

goframe goframe goframe

func Replace

func Replace(origin, search, replace string, count ...int) string

Replace returns a copy of the string `origin` in which string `search` replaced by `replace` case-sensitively.

Example

replace

package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		origin  = `golang is very nice!`
		search  = `golang`
		replace = `goframe`
		result  = gstr.Replace(origin, search, replace)
	)
	fmt.Println(result)

}
Output:

goframe is very nice!

func ReplaceByArray

func ReplaceByArray(origin string, array []string) string

ReplaceByArray returns a copy of `origin`, which is replaced by a slice in order, case-sensitively.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	{
		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

func ReplaceByMap

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

ReplaceByMap returns a copy of `origin`, which is replaced by a map in unordered way, case-sensitively.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	{
		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

func ReplaceI

func ReplaceI(origin, search, replace string, count ...int) string

ReplaceI returns a copy of the string `origin` in which string `search` replaced by `replace` case-insensitively.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		origin  = `golang is very nice!`
		search  = `GOLANG`
		replace = `goframe`
		result  = gstr.ReplaceI(origin, search, replace)
	)
	fmt.Println(result)

}
Output:

goframe is very nice!

func ReplaceIByArray

func ReplaceIByArray(origin string, array []string) string

ReplaceIByArray returns a copy of `origin`, which is replaced by a slice in order, case-insensitively.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	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

func ReplaceIByMap

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

ReplaceIByMap returns a copy of `origin`, which is replaced by a map in unordered way, case-insensitively.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	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

func Reverse

func Reverse(str string) string

Reverse returns a string which is the reverse of `str`.

Example: Reverse("123456") -> "654321"

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		str    = `123456`
		result = gstr.Reverse(str)
	)
	fmt.Println(result)

}
Output:

654321

func SearchArray

func SearchArray(a []string, s string) int

SearchArray searches string `s` in string slice `a` case-sensitively, returns its index in `a`. If `s` is not found in `a`, it returns -1.

Example

array

package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		array  = []string{"goframe", "is", "very", "nice"}
		str    = `goframe`
		result = gstr.SearchArray(array, str)
	)
	fmt.Println(result)

}
Output:

0

func Shuffle

func Shuffle(str string) string

Shuffle randomly shuffles a string. It considers parameter `str` as unicode string.

Example: Shuffle("123456") -> "325164" Shuffle("123456") -> "231546" ...

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		str    = `123456`
		result = gstr.Shuffle(str)
	)
	fmt.Println(result)

	// May Output:
	// 563214
}
Output:

func SimilarText

func SimilarText(first, second string, percent *float64) int

SimilarText calculates the similarity between two strings. See http://php.net/manual/en/function.similar-text.php.

Example

similartext

package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		first   = `AaBbCcDd`
		second  = `ad`
		percent = 0.80
		result  = gstr.SimilarText(first, second, &percent)
	)
	fmt.Println(result)

}
Output:

2

func Soundex

func Soundex(str string) string

Soundex calculates the soundex key of a string. See http://php.net/manual/en/function.soundex.php.

Example

soundex

package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		str1    = `Hello`
		str2    = `Hallo`
		result1 = gstr.Soundex(str1)
		result2 = gstr.Soundex(str2)
	)
	fmt.Println(result1, result2)

}
Output:

H400 H400

func Split

func Split(str, delimiter string) []string

Split splits string `str` by a string `delimiter`, to an array.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		str       = `a|b|c|d`
		delimiter = `|`
		result    = gstr.Split(str, delimiter)
	)
	fmt.Printf(`%#v`, result)

}
Output:

[]string{"a", "b", "c", "d"}

func SplitAndTrim

func SplitAndTrim(str, delimiter string, characterMask ...string) []string

SplitAndTrim splits string `str` by a string `delimiter` to an array, and calls Trim to every element of this array. It ignores the elements which are empty after Trim.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		str       = `a|b|||||c|d`
		delimiter = `|`
		result    = gstr.SplitAndTrim(str, delimiter)
	)
	fmt.Printf(`%#v`, result)

}
Output:

[]string{"a", "b", "c", "d"}

func Str

func Str(haystack string, needle string) string

Str returns part of `haystack` string starting from and including the first occurrence of `needle` to the end of `haystack`.

This function performs exactly as function SubStr, but to implement the same function as PHP: http://php.net/manual/en/function.strstr.php.

Example: Str("av.mp4", ".") -> ".mp4"

Example

str

package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		haystack = `xxx.jpg`
		needle   = `.`
		result   = gstr.Str(haystack, needle)
	)
	fmt.Println(result)

}
Output:

.jpg

func StrEx

func StrEx(haystack string, needle string) string

StrEx returns part of `haystack` string starting from and excluding the first occurrence of `needle` to the end of `haystack`.

This function performs exactly as function SubStrEx, but to implement the same function as PHP: http://php.net/manual/en/function.strstr.php.

Example: StrEx("av.mp4", ".") -> "mp4"

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		haystack = `https://goframe.org/index.html?a=1&b=2`
		needle   = `?`
		result   = gstr.StrEx(haystack, needle)
	)
	fmt.Println(result)

}
Output:

a=1&b=2

func StrLimit

func StrLimit(str string, length int, suffix ...string) string

StrLimit returns a portion of string `str` specified by `length` parameters, if the length of `str` is greater than `length`, then the `suffix` will be appended to the result string.

Example: StrLimit("123456", 3) -> "123..." StrLimit("123456", 3, "~") -> "123~"

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		str    = `123456789`
		length = 3
		suffix = `...`
		result = gstr.StrLimit(str, length, suffix)
	)
	fmt.Println(result)

}
Output:

123...

func StrLimitRune

func StrLimitRune(str string, length int, suffix ...string) string

StrLimitRune returns a portion of string `str` specified by `length` parameters, if the length of `str` is greater than `length`, then the `suffix` will be appended to the result string. StrLimitRune considers parameter `str` as unicode string.

Example: StrLimitRune("一起学习吧!", 2) -> "一起..." StrLimitRune("一起学习吧!", 2, "~") -> "一起~"

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		str    = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架。`
		length = 17
		suffix = "..."
		result = gstr.StrLimitRune(str, length, suffix)
	)
	fmt.Println(result)

}
Output:

GoFrame是一款模块化、高性能...

func StrTill

func StrTill(haystack string, needle string) string

StrTill returns part of `haystack` string ending to and including the first occurrence of `needle` from the start of `haystack`.

Example: StrTill("av.mp4", ".") -> "av."

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		haystack = `https://goframe.org/index.html?test=123456`
		needle   = `?`
		result   = gstr.StrTill(haystack, needle)
	)
	fmt.Println(result)

}
Output:

https://goframe.org/index.html?

func StrTillEx

func StrTillEx(haystack string, needle string) string

StrTillEx returns part of `haystack` string ending to and excluding the first occurrence of `needle` from the start of `haystack`.

Example: StrTillEx("av.mp4", ".") -> "av"

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		haystack = `https://goframe.org/index.html?test=123456`
		needle   = `?`
		result   = gstr.StrTillEx(haystack, needle)
	)
	fmt.Println(result)

}
Output:

https://goframe.org/index.html

func StripSlashes

func StripSlashes(str string) string

StripSlashes un-quotes a quoted string by AddSlashes.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		str    = `C:\\windows\\GoFrame\\test`
		result = gstr.StripSlashes(str)
	)
	fmt.Println(result)

}
Output:

C:\windows\GoFrame\test

func SubStr

func SubStr(str string, start int, length ...int) (substr string)

SubStr returns a portion of string `str` specified by the `start` and `length` parameters. The parameter `length` is optional, it uses the length of `str` in default.

Example: SubStr("123456", 1, 2) -> "23"

Example

substr

package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		str    = `1234567890`
		start  = 0
		length = 4
		subStr = gstr.SubStr(str, start, length)
	)
	fmt.Println(subStr)

}
Output:

1234

func SubStrFrom

func SubStrFrom(str string, need string) (substr string)

SubStrFrom returns a portion of string `str` starting from first occurrence of and including `need` to the end of `str`.

Example: SubStrFrom("av.mp4", ".") -> ".mp4"

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		str  = "我爱GoFrameGood"
		need = `爱`
	)

	fmt.Println(gstr.SubStrFrom(str, need))

}
Output:

爱GoFrameGood

func SubStrFromEx

func SubStrFromEx(str string, need string) (substr string)

SubStrFromEx returns a portion of string `str` starting from first occurrence of and excluding `need` to the end of `str`.

Example: SubStrFromEx("av.mp4", ".") -> "mp4"

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		str  = "我爱GoFrameGood"
		need = `爱`
	)

	fmt.Println(gstr.SubStrFromEx(str, need))

}
Output:

GoFrameGood

func SubStrFromR

func SubStrFromR(str string, need string) (substr string)

SubStrFromR returns a portion of string `str` starting from last occurrence of and including `need` to the end of `str`.

Example: SubStrFromR("/dev/vda", "/") -> "/vda"

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		str  = "我爱GoFrameGood"
		need = `Go`
	)

	fmt.Println(gstr.SubStrFromR(str, need))

}
Output:

Good

func SubStrFromREx

func SubStrFromREx(str string, need string) (substr string)

SubStrFromREx returns a portion of string `str` starting from last occurrence of and excluding `need` to the end of `str`.

Example: SubStrFromREx("/dev/vda", "/") -> "vda"

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		str  = "我爱GoFrameGood"
		need = `Go`
	)

	fmt.Println(gstr.SubStrFromREx(str, need))

}
Output:

od

func SubStrRune

func SubStrRune(str string, start int, length ...int) (substr string)

SubStrRune returns a portion of string `str` specified by the `start` and `length` parameters. SubStrRune considers parameter `str` as unicode string. The parameter `length` is optional, it uses the length of `str` in default.

Example: SubStrRune("一起学习吧!", 2, 2) -> "学习"

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		str    = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架。`
		start  = 14
		length = 3
		subStr = gstr.SubStrRune(str, start, length)
	)
	fmt.Println(subStr)

}
Output:

高性能

func ToLower

func ToLower(s string) string

ToLower returns a copy of the string s with all Unicode letters mapped to their lower case.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		s      = `GOFRAME`
		result = gstr.ToLower(s)
	)
	fmt.Println(result)

}
Output:

goframe

func ToUpper

func ToUpper(s string) string

ToUpper returns a copy of the string s with all Unicode letters mapped to their upper case.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		s      = `goframe`
		result = gstr.ToUpper(s)
	)
	fmt.Println(result)

}
Output:

GOFRAME

func Trim

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

Trim strips whitespace (or other characters) from the beginning and end of a string. The optional parameter `characterMask` specifies the additional stripped characters.

Example

trim

package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

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

}
Output:

Hello World

func TrimAll

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

TrimAll trims all characters in string `str`.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

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

}
Output:

HelloWorld

func TrimLeft

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

TrimLeft strips whitespace (or other characters) from the beginning of a string.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

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

}
Output:

Hello World*

func TrimLeftStr

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

TrimLeftStr strips all the given `cut` string from the beginning of a string. Note that it does not strip the whitespaces of its beginning.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

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

}
Output:

*Hello World**

func TrimRight

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

TrimRight strips whitespace (or other characters) from the end of a string.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

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

}
Output:

**Hello Worl

func TrimRightStr

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

TrimRightStr strips all the given `cut` string from the end of a string. Note that it does not strip the whitespaces of its end.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

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

}
Output:

Hello World

func TrimStr

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

TrimStr strips all the given `cut` string from the beginning and end of a string. Note that it does not strip the whitespaces of its beginning or end.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

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

}
Output:

Hello

func UcFirst

func UcFirst(s string) string

UcFirst returns a copy of the string s with the first letter mapped to its upper case.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		s      = `hello`
		result = gstr.UcFirst(s)
	)
	fmt.Println(result)

}
Output:

Hello

func UcWords

func UcWords(str string) string

UcWords uppercase the first character of each word in a string.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	var (
		str    = `hello world`
		result = gstr.UcWords(str)
	)
	fmt.Println(result)

}
Output:

Hello World

func WordWrap

func WordWrap(str string, width int, br string) string

WordWrap wraps a string to a given number of characters. This function supports cut parameters of both english and chinese punctuations. TODO: Enable custom cut parameter, see http://php.net/manual/en/function.wordwrap.php.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gstr"
)

func main() {
	{
		var (
			str    = `A very long woooooooooooooooooord. and something`
			width  = 8
			br     = "\n"
			result = gstr.WordWrap(str, width, br)
		)
		fmt.Println(result)
	}
	{
		var (
			str    = `The quick brown fox jumped over the lazy dog.`
			width  = 20
			br     = "<br />\n"
			result = gstr.WordWrap(str, width, br)
		)
		fmt.Printf("%v", result)
	}

}
Output:

A very
long
woooooooooooooooooord.
and
something
The quick brown fox<br />
jumped over the lazy<br />
dog.

Types

type CaseType added in v2.5.7

type CaseType string

CaseType is the type for Case.

const (
	Camel           CaseType = "Camel"
	CamelLower      CaseType = "CamelLower"
	Snake           CaseType = "Snake"
	SnakeFirstUpper CaseType = "SnakeFirstUpper"
	SnakeScreaming  CaseType = "SnakeScreaming"
	Kebab           CaseType = "Kebab"
	KebabScreaming  CaseType = "KebabScreaming"
	Lower           CaseType = "Lower"
)

The case type constants.

func CaseTypeMatch added in v2.5.7

func CaseTypeMatch(caseStr string) CaseType

CaseTypeMatch matches the case type from string.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL