gstr provides powerful and convenient text processing components, with a large number of commonly used string processing methods built in, making it more comprehensive and rich compared to the Golang standard library, suitable for dealing with most business scenarios.

Usage:

  1. import "github.com/gogf/gf/v2/text/gstr"

API Documentation:

https://pkg.go.dev/github.com/gogf/gf/v2/text/gstr

String Processing - 图1tip

The list of common methods below might be outdated compared to code updates with new features. For more methods and examples, please refer to the code documentation: https://pkg.go.dev/github.com/gogf/gf/v2/text/gstr

String Judgment

IsNumeric

  • Description: IsNumeric verifies whether the string s is a number.

  • Format:

  1. IsNumeric(s string) bool
  • Example:
  1. func ExampleIsNumeric() {
  2. fmt.Println(gstr.IsNumeric("88"))
  3. fmt.Println(gstr.IsNumeric("3.1415926"))
  4. fmt.Println(gstr.IsNumeric("abc"))
  5. // Output:
  6. // true
  7. // true
  8. // false
  9. }

String Length

LenRune

  • Description: LenRune returns the length of a unicode string.

  • Format:

  1. LenRune(str string) int
  • Example:
  1. func ExampleLenRune() {
  2. var (
  3. str = `GoFrame框架`
  4. result = gstr.LenRune(str)
  5. )
  6. fmt.Println(result)
  7. // Output:
  8. // 9
  9. }

String Creation

Repeat

  • Description: Repeat returns a new string consisting of the input string repeated multiplier times.

  • Format:

  1. Repeat(input string, multiplier int) string
  • Example:
  1. func ExampleRepeat() {
  2. var (
  3. input = `goframe `
  4. multiplier = 3
  5. result = gstr.Repeat(input, multiplier)
  6. )
  7. fmt.Println(result)
  8. // Output:
  9. // goframe goframe goframe
  10. }

Case Conversion

ToLower

  • Description: ToLower converts all Unicode characters in s to lowercase and returns its copy.

  • Format:

  1. ToLower(s string) string
  • Example:
  1. func ExampleToLower() {
  2. var (
  3. s = `GOFRAME`
  4. result = gstr.ToLower(s)
  5. )
  6. fmt.Println(result)
  7. // Output:
  8. // goframe
  9. }

ToUpper

  • Description: ToUpper converts all Unicode characters in s to uppercase and returns its copy.

  • Format:

  1. ToUpper(s string) string
  • Example:
  1. func ExampleToUpper() {
  2. var (
  3. s = `goframe`
  4. result = gstr.ToUpper(s)
  5. )
  6. fmt.Println(result)
  7. // Output:
  8. // GOFRAME
  9. }

UcFirst

  • Description: UcFirst capitalizes the first character of s and returns its copy.

  • Format:

  1. UcFirst(s string) string
  • Example:
  1. func ExampleUcFirst() {
  2. var (
  3. s = `hello`
  4. result = gstr.UcFirst(s)
  5. )
  6. fmt.Println(result)
  7. // Output:
  8. // Hello
  9. }

LcFirst

  • Description: LcFirst converts the first character of s to lowercase and returns its copy.

  • Format:

  1. LcFirst(s string) string
  • Example:
  1. func ExampleLcFirst() {
  2. var (
  3. str = `Goframe`
  4. result = gstr.LcFirst(str)
  5. )
  6. fmt.Println(result)
  7. // Output:
  8. // goframe
  9. }

UcWords

  • Description: UcWords capitalizes the first character of each word in the string str.

  • Format:

  1. UcWords(str string) string
  • Example:
  1. func ExampleUcWords() {
  2. var (
  3. str = `hello world`
  4. result = gstr.UcWords(str)
  5. )
  6. fmt.Println(result)
  7. // Output:
  8. // Hello World
  9. }

IsLetterLower

  • Description: IsLetterLower verifies whether the given character b is lowercase.

  • Format:

  1. IsLetterLower(b byte) bool
  • Example:
  1. func ExampleIsLetterLower() {
  2. fmt.Println(gstr.IsLetterLower('a'))
  3. fmt.Println(gstr.IsLetterLower('A'))
  4. // Output:
  5. // true
  6. // false
  7. }

IsLetterUpper

  • Description: IsLetterUpper verifies whether the character b is uppercase.

  • Format:

  1. IsLetterUpper(b byte) bool
  • Example:
  1. func ExampleIsLetterUpper() {
  2. fmt.Println(gstr.IsLetterUpper('A'))
  3. fmt.Println(gstr.IsLetterUpper('a'))
  4. // Output:
  5. // true
  6. // false
  7. }

String Comparison

Compare

  • Description: Compare returns an integer comparing two strings lexicographically. If a == b, it returns 0; if a < b, it returns -1; if a > b, it returns +1.

  • Format:

  1. Compare(a, b string) int
  • Example:
  1. func ExampleCompare() {
  2. fmt.Println(gstr.Compare("c", "c"))
  3. fmt.Println(gstr.Compare("a", "b"))
  4. fmt.Println(gstr.Compare("c", "b"))
  5. // Output:
  6. // 0
  7. // -1
  8. // 1
  9. }

Equal

  • Description: Equal returns whether a and b are equal, case-insensitively.

  • Format:

  1. Equal(a, b string) bool
  • Example:
  1. func ExampleEqual() {
  2. fmt.Println(gstr.Equal(`A`, `a`))
  3. fmt.Println(gstr.Equal(`A`, `A`))
  4. fmt.Println(gstr.Equal(`A`, `B`))
  5. // Output:
  6. // true
  7. // true
  8. // false
  9. }

Split and Join

Split

  • Description: Split splits str into []string using delimiter.

  • Format:

  1. Split(str, delimiter string) []string
  • Example:
  1. func ExampleSplit() {
  2. var (
  3. str = `a|b|c|d`
  4. delimiter = `|`
  5. result = gstr.Split(str, delimiter)
  6. )
  7. fmt.Printf(`%#v`, result)
  8. // Output:
  9. // []string{"a", "b", "c", "d"}
  10. }

SplitAndTrim

  • Description: SplitAndTrim splits str into []string using delimiter and calls Trim on each element of []string, ignoring elements that are empty after Trim.

  • Format:

  1. SplitAndTrim(str, delimiter string, characterMask ...string) []string
  • Example:
  1. func ExampleSplitAndTrim() {
  2. var (
  3. str = `a|b|||||c|d`
  4. delimiter = `|`
  5. result = gstr.SplitAndTrim(str, delimiter)
  6. )
  7. fmt.Printf(`%#v`, result)
  8. // Output:
  9. // []string{"a", "b", "c", "d"}
  10. }

Join

  • Description: Join concatenates each element of array into a new string. The parameter sep is used as the separator for the new string.

  • Format:

  1. Join(array []string, sep string) string
  • Example:
  1. func ExampleJoin() {
  2. var (
  3. array = []string{"goframe", "is", "very", "easy", "to", "use"}
  4. sep = ` `
  5. result = gstr.Join(array, sep)
  6. )
  7. fmt.Println(result)
  8. // Output:
  9. // goframe is very easy to use
  10. }

JoinAny

  • Description: JoinAny concatenates each element of array into a new string. The parameter sep is used as the separator for the new string. The parameter array can be of any type.

  • Format:

  1. JoinAny(array interface{}, sep string) string
  • Example:
  1. func ExampleJoinAny() {
  2. var (
  3. sep = `,`
  4. arr2 = []int{99, 73, 85, 66}
  5. result = gstr.JoinAny(arr2, sep)
  6. )
  7. fmt.Println(result)
  8. // Output:
  9. // 99,73,85,66
  10. }

Explode

  • Description: Explode splits str into []string using the delimiter delimiter.

  • Format:

  1. Explode(delimiter, str string) []string
  • Example:
  1. func ExampleExplode() {
  2. var (
  3. str = `Hello World`
  4. delimiter = " "
  5. result = gstr.Explode(delimiter, str)
  6. )
  7. fmt.Printf(`%#v`, result)
  8. // Output:
  9. // []string{"Hello", "World"}
  10. }

Implode

  • Description: Implode joins each element of the pieces string array using glue.

  • Format:

  1. Implode(glue string, pieces []string) string
  • Example:
  1. func ExampleImplode() {
  2. var (
  3. pieces = []string{"goframe", "is", "very", "easy", "to", "use"}
  4. glue = " "
  5. result = gstr.Implode(glue, pieces)
  6. )
  7. fmt.Println(result)
  8. // Output:
  9. // goframe is very easy to use
  10. }

ChunkSplit

  • Description: ChunkSplit splits the string into smaller chunks of chunkLen and joins each chunk with end.

  • Format:

  1. ChunkSplit(body string, chunkLen int, end string) string
  • Example:
  1. func ExampleChunkSplit() {
  2. var (
  3. body = `1234567890`
  4. chunkLen = 2
  5. end = "#"
  6. result = gstr.ChunkSplit(body, chunkLen, end)
  7. )
  8. fmt.Println(result)
  9. // Output:
  10. // 12#34#56#78#90#
  11. }

Fields

  • Description: Fields returns []string representing each word in the string.

  • Format:

  1. Fields(str string) []string
  • Example:
  1. func ExampleFields() {
  2. var (
  3. str = `Hello World`
  4. result = gstr.Fields(str)
  5. )
  6. fmt.Printf(`%#v`, result)
  7. // Output:
  8. // []string{"Hello", "World"}
  9. }

Escape Processing

AddSlashes

  • Description: AddSlashes adds an escape character '\' before the symbols in the string.

  • Format:

  1. AddSlashes(str string) string
  • Example:
  1. func ExampleAddSlashes() {
  2. var (
  3. str = `'aa'"bb"cc\r\n\d\t`
  4. result = gstr.AddSlashes(str)
  5. )
  6. fmt.Println(result)
  7. // Output:
  8. // \'aa\'\"bb\"cc\\r\\n\\d\\t
  9. }

StripSlashes

  • Description: StripSlashes removes the escape characters '\' from string str.

  • Format:

  1. StripSlashes(str string) string
  • Example:
  1. func ExampleStripSlashes() {
  2. var (
  3. str = `C:\\windows\\GoFrame\\test`
  4. result = gstr.StripSlashes(str)
  5. )
  6. fmt.Println(result)
  7. // Output:
  8. // C:\windows\GoFrame\test
  9. }

QuoteMeta

  • Description: QuoteMeta adds an escape character '\' before each character in str among ‘ \ + * ? [ ^ ] ( $ ).

  • Format:

  1. QuoteMeta(str string, chars ...string) string
  • Example:
  1. func ExampleQuoteMeta() {
  2. {
  3. var (
  4. str = `.\+?[^]()`
  5. result = gstr.QuoteMeta(str)
  6. )
  7. fmt.Println(result)
  8. }
  9. {
  10. var (
  11. str = `https://goframe.org/pages/viewpage.action?pageId=1114327`
  12. result = gstr.QuoteMeta(str)
  13. )
  14. fmt.Println(result)
  15. }
  16. // Output:
  17. // \.\\\+\?\[\^\]\(\)
  18. // https://goframe\.org/pages/viewpage\.action\?pageId=1114327
  19. }

Count Statistics

Count

  • Description: Count calculates the number of times substr occurs in s. If substr is not found in s, it returns 0.

  • Format:

  1. Count(s, substr string) int
  • Example:
  1. func ExampleCount() {
  2. var (
  3. str = `goframe is very, very easy to use`
  4. substr1 = "goframe"
  5. substr2 = "very"
  6. result1 = gstr.Count(str, substr1)
  7. result2 = gstr.Count(str, substr2)
  8. )
  9. fmt.Println(result1)
  10. fmt.Println(result2)
  11. // Output:
  12. // 1
  13. // 2
  14. }

CountI

  • Description: Count calculates the number of times substr occurs in s, case-insensitively. If not found, returns 0.

  • Format:

  1. CountI(s, substr string) int
  • Example:
  1. func ExampleCountI() {
  2. var (
  3. str = `goframe is very, very easy to use`
  4. substr1 = "GOFRAME"
  5. substr2 = "VERY"
  6. result1 = gstr.CountI(str, substr1)
  7. result2 = gstr.CountI(str, substr2)
  8. )
  9. fmt.Println(result1)
  10. fmt.Println(result2)
  11. // Output:
  12. // 1
  13. // 2
  14. }

CountWords

  • Description: CountWords returns a map[string]int with statistics of words used in str.

  • Format:

  1. CountWords(str string) map[string]int
  • Example:
  1. func ExampleCountWords() {
  2. var (
  3. str = `goframe is very, very easy to use!`
  4. result = gstr.CountWords(str)
  5. )
  6. fmt.Printf(`%#v`, result)
  7. // Output:
  8. // map[string]int{"easy":1, "goframe":1, "is":1, "to":1, "use!":1, "very":1, "very,":1}
  9. }

CountChars

  • Description: CountChars returns statistics of characters used in str as map[string]int. The noSpace parameter controls whether to count spaces.

  • Format:

  1. CountChars(str string, noSpace ...bool) map[string]int
  • Example:
  1. func ExampleCountChars() {
  2. var (
  3. str = `goframe`
  4. result = gstr.CountChars(str)
  5. )
  6. fmt.Println(result)
  7. // May Output:
  8. // map[a:1 e:1 f:1 g:1 m:1 o:1 r:1]
  9. }

Array Processing

SearchArray

  • Description: SearchArray searches string 's' in []string 'a' case-sensitively and returns its index in 'a'. If 's' is not found, it returns -1.

  • Format:

  1. SearchArray(a []string, s string) int
  • Example:
  1. func ExampleSearchArray() {
  2. var (
  3. array = []string{"goframe", "is", "very", "nice"}
  4. str = `goframe`
  5. result = gstr.SearchArray(array, str)
  6. )
  7. fmt.Println(result)
  8. // Output:
  9. // 0
  10. }

InArray

  • Description: InArray verifies whether the []string 'a' contains the string ' s '.

  • Format:

  1. InArray(a []string, s string) bool
  • Example:
  1. func ExampleInArray() {
  2. var (
  3. a = []string{"goframe", "is", "very", "easy", "to", "use"}
  4. s = "goframe"
  5. result = gstr.InArray(a, s)
  6. )
  7. fmt.Println(result)
  8. // Output:
  9. // true
  10. }

PrefixArray

  • Description: PrefixArray adds the prefix 'prefix' to each string in []string array.

  • Format:

  1. PrefixArray(array []string, prefix string)
  • Example:
  1. func ExamplePrefixArray() {
  2. var (
  3. strArray = []string{"tom", "lily", "john"}
  4. )
  5. gstr.PrefixArray(strArray, "classA_")
  6. fmt.Println(strArray)
  7. // Output:
  8. // [classA_tom classA_lily classA_john]
  9. }

Naming Conversion

CaseCamel

  • Description: CaseCamel converts a string to a camel case format (capitalize the first letter).

  • Format:

  1. CaseCamel(s string) string
  • Example:
  1. func ExampleCaseCamel() {
  2. var (
  3. str = `hello world`
  4. result = gstr.CaseCamel(str)
  5. )
  6. fmt.Println(result)
  7. // Output:
  8. // HelloWorld
  9. }

CaseCamelLower

  • Description: CaseCamelLower converts a string to camel case format (lowercase the first letter).

  • Format:

  1. CaseCamelLower(s string) string
  • Example:
  1. func ExampleCaseCamelLower() {
  2. var (
  3. str = `hello world`
  4. result = gstr.CaseCamelLower(str)
  5. )
  6. fmt.Println(result)
  7. // Output:
  8. // helloWorld
  9. }

CaseSnake

  • Description: CaseSnake replaces symbols (underscore, space, dot, hyphen) in a string with underscores (_) and converts all to lowercase.

  • Format:

  1. CaseSnake(s string) string
  • Example:
  1. func ExampleCaseSnake() {
  2. var (
  3. str = `hello world`
  4. result = gstr.CaseSnake(str)
  5. )
  6. fmt.Println(result)
  7. // Output:
  8. // hello_world
  9. }

CaseSnakeScreaming

  • Description: CaseSnakeScreaming replaces symbols (underscore, space, dot, hyphen) in a string with underscores ('_') and converts all letters to uppercase.

  • Format:

  1. CaseSnakeScreaming(s string) string
  • Example:
  1. func ExampleCaseSnakeScreaming() {
  2. var (
  3. str = `hello world`
  4. result = gstr.CaseSnakeScreaming(str)
  5. )
  6. fmt.Println(result)
  7. // Output:
  8. // HELLO_WORLD
  9. }

CaseSnakeFirstUpper

  • Description: CaseSnakeFirstUpper converts uppercase letters to lowercase and adds an underscore '_' before them, except the first uppercase letter, which is only converted to lowercase without an underscore.

  • Format:

  1. CaseSnakeFirstUpper(word string, underscore ...string) string
  • Example:
  1. func ExampleCaseSnakeFirstUpper() {
  2. var (
  3. str = `RGBCodeMd5`
  4. result = gstr.CaseSnakeFirstUpper(str)
  5. )
  6. fmt.Println(result)
  7. // Output:
  8. // rgb_code_md5
  9. }

CaseKebab

  • Description: CaseKebab replaces symbols (underscore, space, dot,) in a string with hyphens ('-') and converts all to lowercase.

  • Format:

  1. CaseKebab(s string) string
  • Example:
  1. func ExampleCaseKebab() {
  2. var (
  3. str = `hello world`
  4. result = gstr.CaseKebab(str)
  5. )
  6. fmt.Println(result)
  7. // Output:
  8. // hello-world
  9. }

CaseKebabScreaming

  • Description: CaseKebabScreaming replaces symbols (underscore, space, dot, hyphen) in a string with hyphens ('-') and converts all to uppercase.

  • Format:

  1. CaseKebabScreaming(s string) string
  • Example:
  1. func ExampleCaseKebabScreaming() {
  2. var (
  3. str = `hello world`
  4. result = gstr.CaseKebabScreaming(str)
  5. )
  6. fmt.Println(result)
  7. // Output:
  8. // HELLO-WORLD
  9. }

CaseDelimited

  • Description: CaseDelimited replaces symbols in a string based on specified delimiters.

  • Format:

  1. CaseDelimited(s string, del byte) string
  • Example:
  1. func ExampleCaseDelimited() {
  2. var (
  3. str = `hello world`
  4. del = byte('-')
  5. result = gstr.CaseDelimited(str, del)
  6. )
  7. fmt.Println(result)
  8. // Output:
  9. // hello-world
  10. }

CaseDelimitedScreaming

  • Description: CaseDelimitedScreaming replaces symbols (space, underscore, dot, hyphen) in a string with the second parameter delimiter, converting to uppercase if the third parameter is true, or to lowercase if false.

  • Format:

  1. CaseDelimitedScreaming(s string, del uint8, screaming bool) string
  • Example:
  1. func ExampleCaseDelimitedScreaming() {
  2. {
  3. var (
  4. str = `hello world`
  5. del = byte('-')
  6. result = gstr.CaseDelimitedScreaming(str, del, true)
  7. )
  8. fmt.Println(result)
  9. }
  10. {
  11. var (
  12. str = `hello world`
  13. del = byte('-')
  14. result = gstr.CaseDelimitedScreaming(str, del, false)
  15. )
  16. fmt.Println(result)
  17. }
  18. // Output:
  19. // HELLO-WORLD
  20. // hello-world
  21. }

Contains Check

Contains

  • Description: Contains returns whether the string str contains the substring substr, case-sensitively.

  • Format:

  1. Contains(str, substr string) bool
  • Example:
  1. func ExampleContains() {
  2. {
  3. var (
  4. str = `Hello World`
  5. substr = `Hello`
  6. result = gstr.Contains(str, substr)
  7. )
  8. fmt.Println(result)
  9. }
  10. {
  11. var (
  12. str = `Hello World`
  13. substr = `hello`
  14. result = gstr.Contains(str, substr)
  15. )
  16. fmt.Println(result)
  17. }
  18. // Output:
  19. // true
  20. // false
  21. }

ContainsI

  • Description: ContainsI verifies whether substr is in str, case-insensitively.

  • Format:

  1. ContainsI(str, substr string) bool
  • Example:
  1. func ExampleContainsI() {
  2. var (
  3. str = `Hello World`
  4. substr = "hello"
  5. result1 = gstr.Contains(str, substr)
  6. result2 = gstr.ContainsI(str, substr)
  7. )
  8. fmt.Println(result1)
  9. fmt.Println(result2)
  10. // Output:
  11. // false
  12. // true
  13. }

ContainsAny

  • Description: ContainsAny verifies whether s contains chars.

  • Format:

  1. ContainsAny(s, chars string) bool
  • Example:
  1. func ExampleContainsAny() {
  2. {
  3. var (
  4. s = `goframe`
  5. chars = "g"
  6. result = gstr.ContainsAny(s, chars)
  7. )
  8. fmt.Println(result)
  9. }
  10. {
  11. var (
  12. s = `goframe`
  13. chars = "G"
  14. result = gstr.ContainsAny(s, chars)
  15. )
  16. fmt.Println(result)
  17. }
  18. // Output:
  19. // true
  20. // false
  21. }

String Conversion

Chr

  • Description: Chr returns the ascii character string for a number 0-255.

  • Format:

  1. Chr(ascii int) string
  • Example:
  1. func ExampleChr() {
  2. var (
  3. ascii = 65 // A
  4. result = gstr.Chr(ascii)
  5. )
  6. fmt.Println(result)
  7. // Output:
  8. // A
  9. }

Ord

  • Description: Ord converts the first byte of a string to a value between 0-255.

  • Format:

  1. Ord(char string) int
  • Example:
  1. func ExampleOrd() {
  2. var (
  3. str = `goframe`
  4. result = gstr.Ord(str)
  5. )
  6. fmt.Println(result)
  7. // Output:
  8. // 103
  9. }

OctStr

  • Description: OctStr converts an octal string in str to its original string form.

  • Format:

  1. OctStr(str string) string
  • Example:
  1. func ExampleOctStr() {
  2. var (
  3. str = `\346\200\241`
  4. result = gstr.OctStr(str)
  5. )
  6. fmt.Println(result)
  7. // Output:
  8. // 怡
  9. }

Reverse

  • Description: Reverse returns the reversed string of str.

  • Format:

  1. Reverse(str string) string
  • Example:
  1. func ExampleReverse() {
  2. var (
  3. str = `123456`
  4. result = gstr.Reverse(str)
  5. )
  6. fmt.Println(result)
  7. // Output:
  8. // 654321
  9. }

NumberFormat

  • Description: NumberFormat formats a number with thousand separators.

    • Parameter decimal sets the number of decimal points.
    • Parameter decPoint sets the decimal point separator.
    • Parameter thousand sets the thousand separator.
  • Format:
  1. NumberFormat(number float64, decimals int, decPoint, thousandsSep string) string
  • Example:
  1. func ExampleNumberFormat() {
  2. var (
  3. number float64 = 123456
  4. decimals = 2
  5. decPoint = "."
  6. thousandsSep = ","
  7. result = gstr.NumberFormat(number, decimals, decPoint, thousandsSep)
  8. )
  9. fmt.Println(result)
  10. // Output:
  11. // 123,456.00
  12. }

Shuffle

  • Description: Shuffle returns a randomly shuffled version of str.

  • Format:

  1. Shuffle(str string) string
  • Example:
  1. func ExampleShuffle() {
  2. var (
  3. str = `123456`
  4. result = gstr.Shuffle(str)
  5. )
  6. fmt.Println(result)
  7. // May Output:
  8. // 563214
  9. }

HideStr

  • Description: HideStr converts a percentage percent of characters in str, starting from the middle, to the hide string.

  • Format:

  1. HideStr(str string, percent int, hide string) string
  • Example:
  1. func ExampleHideStr() {
  2. var (
  3. str = `13800138000`
  4. percent = 40
  5. hide = `*`
  6. result = gstr.HideStr(str, percent, hide)
  7. )
  8. fmt.Println(result)
  9. // Output:
  10. // 138****8000
  11. }

Nl2Br

  • Description: Nl2Br inserts HTML line break (' br ' |<br />) before all newline characters in a string: \n\r, \r\n, \r, \n.

  • Format:

  1. Nl2Br(str string, isXhtml ...bool) string
  • Example:
  1. func ExampleNl2Br() {
  2. var (
  3. str = `goframe
  4. is
  5. very
  6. easy
  7. to
  8. use`
  9. result = gstr.Nl2Br(str)
  10. )
  11. fmt.Println(result)
  12. // Output:
  13. // goframe<br>is<br>very<br>easy<br>to<br>use
  14. }

WordWrap

  • Description: WordWrap uses line breaks to wrap str to a given number of characters (without splitting words).

  • Format:

  1. WordWrap(str string, width int, br string) string
  • Example:
  1. func ExampleWordWrap() {
  2. {
  3. var (
  4. str = `A very long woooooooooooooooooord. and something`
  5. width = 8
  6. br = "\n"
  7. result = gstr.WordWrap(str, width, br)
  8. )
  9. fmt.Println(result)
  10. }
  11. {
  12. var (
  13. str = `The quick brown fox jumped over the lazy dog.`
  14. width = 20
  15. br = "<br />\n"
  16. result = gstr.WordWrap(str, width, br)
  17. )
  18. fmt.Printf("%v", result)
  19. }
  20. // Output:
  21. // A very
  22. // long
  23. // woooooooooooooooooord.
  24. // and
  25. // something
  26. // The quick brown fox<br />
  27. // jumped over the lazy<br />
  28. // dog.
  29. }

Domain Processing

IsSubDomain

  • Description: IsSubDomain verifies whether subDomain is a subdomain of mainDomain. Supports '*' in mainDomain.

  • Format:

  1. IsSubDomain(subDomain string, mainDomain string) bool
  • Example:
  1. func ExampleIsSubDomain() {
  2. var (
  3. subDomain = `s.goframe.org`
  4. mainDomain = `goframe.org`
  5. result = gstr.IsSubDomain(subDomain, mainDomain)
  6. )
  7. fmt.Println(result)
  8. // Output:
  9. // true
  10. }

Parameter Parsing

Parse

  • Description: Parse parses a string and returns it as map[string]interface{}.

  • Format:

  1. Parse(s string) (result map[string]interface{}, err error)
  • Example:
  1. func ExampleParse() {
  2. {
  3. var (
  4. str = `v1=m&v2=n`
  5. result, _ = gstr.Parse(str)
  6. )
  7. fmt.Println(result)
  8. }
  9. {
  10. var (
  11. str = `v[a][a]=m&v[a][b]=n`
  12. result, _ = gstr.Parse(str)
  13. )
  14. fmt.Println(result)
  15. }
  16. {
  17. // The form of nested Slice is not yet supported.
  18. var str = `v[][]=m&v[][]=n`
  19. result, err := gstr.Parse(str)
  20. if err != nil {
  21. panic(err)
  22. }
  23. fmt.Println(result)
  24. }
  25. {
  26. // This will produce an error.
  27. var str = `v=m&v[a]=n`
  28. result, err := gstr.Parse(str)
  29. if err != nil {
  30. println(err)
  31. }
  32. fmt.Println(result)
  33. }
  34. {
  35. var (
  36. str = `a .[[b=c`
  37. result, _ = gstr.Parse(str)
  38. )
  39. fmt.Println(result)
  40. }
  41. // May Output:
  42. // map[v1:m v2:n]
  43. // map[v:map[a:map[a:m b:n]]]
  44. // map[v:map[]]
  45. // Error: expected type 'map[string]interface{}' for key 'v', but got 'string'
  46. // map[]
  47. // map[a___[b:c]
  48. }

Position Finding

Pos

  • Description: Pos returns the first occurrence of needle in haystack, case-sensitively. If not found, returns -1.

  • Format:

  1. Pos(haystack, needle string, startOffset ...int) int
  • Example:
  1. func ExamplePos() {
  2. var (
  3. haystack = `Hello World`
  4. needle = `World`
  5. result = gstr.Pos(haystack, needle)
  6. )
  7. fmt.Println(result)
  8. // Output:
  9. // 6
  10. }

PosRune

  • Description: PosRune functions similarly to Pos, but supports haystack and needle as unicode strings.

  • Format:

  1. PosRune(haystack, needle string, startOffset ...int) int
  • Example:
  1. func ExamplePosRune() {
  2. var (
  3. haystack = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架`
  4. needle = `Go`
  5. posI = gstr.PosRune(haystack, needle)
  6. posR = gstr.PosRRune(haystack, needle)
  7. )
  8. fmt.Println(posI)
  9. fmt.Println(posR)
  10. // Output:
  11. // 0
  12. // 22
  13. }

PosI

  • Description: PosI returns the first occurrence of needle in haystack, case-insensitively. If not found, returns -1.

  • Format:

  1. PosI(haystack, needle string, startOffset ...int) int
  • Example:
  1. func ExamplePosI() {
  2. var (
  3. haystack = `goframe is very, very easy to use`
  4. needle = `very`
  5. posI = gstr.PosI(haystack, needle)
  6. posR = gstr.PosR(haystack, needle)
  7. )
  8. fmt.Println(posI)
  9. fmt.Println(posR)
  10. // Output:
  11. // 11
  12. // 17
  13. }

PosRuneI

  • Description: PosRuneI functions similarly to PosI, but supports haystack and needle as unicode strings.

  • Format:

  1. PosIRune(haystack, needle string, startOffset ...int) int
  • Example:
  1. func ExamplePosIRune() {
  2. {
  3. var (
  4. haystack = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架`
  5. needle = `高性能`
  6. startOffset = 10
  7. result = gstr.PosIRune(haystack, needle, startOffset)
  8. )
  9. fmt.Println(result)
  10. }
  11. {
  12. var (
  13. haystack = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架`
  14. needle = `高性能`
  15. startOffset = 30
  16. result = gstr.PosIRune(haystack, needle, startOffset)
  17. )
  18. fmt.Println(result)
  19. }
  20. // Output:
  21. // 14
  22. // -1
  23. }

PosR

  • Description: PosR returns the last occurrence of needle in haystack, case-sensitively. If not found, returns -1.

  • Format:

  1. PosR(haystack, needle string, startOffset ...int) int
  • Example:
  1. func ExamplePosR() {
  2. var (
  3. haystack = `goframe is very, very easy to use`
  4. needle = `very`
  5. posI = gstr.PosI(haystack, needle)
  6. posR = gstr.PosR(haystack, needle)
  7. )
  8. fmt.Println(posI)
  9. fmt.Println(posR)
  10. // Output:
  11. // 11
  12. // 17
  13. }

PosRuneR

  • Description: PosRuneR functions similarly to PosR, but supports haystack and needle as unicode strings.

  • Format:

  1. PosRRune(haystack, needle string, startOffset ...int) int
  • Example:
  1. func ExamplePosRRune() {
  2. var (
  3. haystack = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架`
  4. needle = `Go`
  5. posI = gstr.PosIRune(haystack, needle)
  6. posR = gstr.PosRRune(haystack, needle)
  7. )
  8. fmt.Println(posI)
  9. fmt.Println(posR)
  10. // Output:
  11. // 0
  12. // 22
  13. }

PosRI

  • Description: PosRI returns the last occurrence of needle in haystack, case-insensitively. If not found, returns -1.

  • Format:

  1. PosRI(haystack, needle string, startOffset ...int) int
  • Example:
  1. func ExamplePosRI() {
  2. var (
  3. haystack = `goframe is very, very easy to use`
  4. needle = `VERY`
  5. posI = gstr.PosI(haystack, needle)
  6. posR = gstr.PosRI(haystack, needle)
  7. )
  8. fmt.Println(posI)
  9. fmt.Println(posR)
  10. // Output:
  11. // 11
  12. // 17
  13. }

PosRIRune

  • Description: PosRIRune functions similarly to PosRI, but supports haystack and needle as unicode strings.

  • Format:

  1. PosRIRune(haystack, needle string, startOffset ...int) int
  • Example:
  1. func ExamplePosRIRune() {
  2. var (
  3. haystack = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架`
  4. needle = `GO`
  5. posI = gstr.PosIRune(haystack, needle)
  6. posR = gstr.PosRIRune(haystack, needle)
  7. )
  8. fmt.Println(posI)
  9. fmt.Println(posR)
  10. // Output:
  11. // 0
  12. // 22
  13. }

Find and Replace

Replace

  • Description: Replace returns a new string where search in origin is replaced by replace. search is case-sensitive.

  • Format:

  1. Replace(origin, search, replace string, count ...int) string
  • Example:
  1. func ExampleReplace() {
  2. var (
  3. origin = `golang is very nice!`
  4. search = `golang`
  5. replace = `goframe`
  6. result = gstr.Replace(origin, search, replace)
  7. )
  8. fmt.Println(result)
  9. // Output:
  10. // goframe is very nice!
  11. }

ReplaceI

  • Description: ReplaceI returns a new string where search in origin is replaced by replace. search is case-insensitive.

  • Format:

  1. ReplaceI(origin, search, replace string, count ...int) string
  • Example:
  1. func ExampleReplaceI() {
  2. var (
  3. origin = `golang is very nice!`
  4. search = `GOLANG`
  5. replace = `goframe`
  6. result = gstr.ReplaceI(origin, search, replace)
  7. )
  8. fmt.Println(result)
  9. // Output:
  10. // goframe is very nice!
  11. }

ReplaceByArray

  • Description: ReplaceByArray returns a new string where origin is sequentially replaced with pairs (search, replace) from an array, case-sensitively.

  • Format:

  1. ReplaceByArray(origin string, array []string) string
  • Example:
  1. func ExampleReplaceByArray() {
  2. {
  3. var (
  4. origin = `golang is very nice`
  5. array = []string{"lang", "frame"}
  6. result = gstr.ReplaceByArray(origin, array)
  7. )
  8. fmt.Println(result)
  9. }
  10. {
  11. var (
  12. origin = `golang is very good`
  13. array = []string{"golang", "goframe", "good", "nice"}
  14. result = gstr.ReplaceByArray(origin, array)
  15. )
  16. fmt.Println(result)
  17. }
  18. // Output:
  19. // goframe is very nice
  20. // goframe is very nice
  21. }

ReplaceIByArray

  • Description: ReplaceIByArray returns a new string where origin is sequentially replaced with pairs (search, replace) from an array, case-insensitively.

  • Format:

  1. ReplaceIByArray(origin string, array []string) string
  • Example:
  1. func ExampleReplaceIByArray() {
  2. var (
  3. origin = `golang is very Good`
  4. array = []string{"Golang", "goframe", "GOOD", "nice"}
  5. result = gstr.ReplaceIByArray(origin, array)
  6. )
  7. fmt.Println(result)
  8. // Output:
  9. // goframe is very nice
  10. }

ReplaceByMap

  • Description: ReplaceByMap returns a new string where keys in map are replaced with values in origin, case-sensitively.

  • Format:

  1. ReplaceByMap(origin string, replaces map[string]string) string
  • Example:
  1. func ExampleReplaceByMap() {
  2. {
  3. var (
  4. origin = `golang is very nice`
  5. replaces = map[string]string{
  6. "lang": "frame",
  7. }
  8. result = gstr.ReplaceByMap(origin, replaces)
  9. )
  10. fmt.Println(result)
  11. }
  12. {
  13. var (
  14. origin = `golang is very good`
  15. replaces = map[string]string{
  16. "golang": "goframe",
  17. "good": "nice",
  18. }
  19. result = gstr.ReplaceByMap(origin, replaces)
  20. )
  21. fmt.Println(result)
  22. }
  23. // Output:
  24. // goframe is very nice
  25. // goframe is very nice
  26. }

ReplaceIByMap

  • Description: ReplaceIByMap returns a new string where keys in map are replaced with values in origin, case-insensitively.

  • Format:

  1. ReplaceIByMap(origin string, replaces map[string]string) string
  • Example:
  1. func ExampleReplaceIByMap() {
  2. var (
  3. origin = `golang is very nice`
  4. replaces = map[string]string{
  5. "Lang": "frame",
  6. }
  7. result = gstr.ReplaceIByMap(origin, replaces)
  8. )
  9. fmt.Println(result)
  10. // Output:
  11. // goframe is very nice
  12. }

Substring Extraction

Str

  • Description: Str returns the substring from the first occurrence of needle to the end of haystack (including needle itself).

  • Format:

  1. Str(haystack string, needle string) string
  • Example:
  1. func ExampleStr() {
  2. var (
  3. haystack = `xxx.jpg`
  4. needle = `.`
  5. result = gstr.Str(haystack, needle)
  6. )
  7. fmt.Println(result)
  8. // Output:
  9. // .jpg
  10. }

StrEx

  • Description: StrEx returns the substring from the first occurrence of needle to the end of haystack (excluding needle itself).

  • Format:

  1. StrEx(haystack string, needle string) string
  • Example:
  1. func ExampleStrEx() {
  2. var (
  3. haystack = `https://goframe.org/index.html?a=1&b=2`
  4. needle = `?`
  5. result = gstr.StrEx(haystack, needle)
  6. )
  7. fmt.Println(result)
  8. // Output:
  9. // a=1&b=2
  10. }

StrTill

  • Description: StrTill returns the substring from the start of haystack to the first occurrence of needle (including needle itself).

  • Format:

  1. StrTill(haystack string, needle string) string
  • Example:
  1. func ExampleStrTill() {
  2. var (
  3. haystack = `https://goframe.org/index.html?test=123456`
  4. needle = `?`
  5. result = gstr.StrTill(haystack, needle)
  6. )
  7. fmt.Println(result)
  8. // Output:
  9. // https://goframe.org/index.html?
  10. }

StrTillEx

  • Description: StrTillEx returns the substring from the start of haystack to the first occurrence of needle (excluding needle itself).

  • Format:

  1. StrTillEx(haystack string, needle string) string
  • Example:
  1. func ExampleStrTillEx() {
  2. var (
  3. haystack = `https://goframe.org/index.html?test=123456`
  4. needle = `?`
  5. result = gstr.StrTillEx(haystack, needle)
  6. )
  7. fmt.Println(result)
  8. // Output:
  9. // https://goframe.org/index.html
  10. }

SubStr

  • Description: SubStr returns a new substring from str starting at start with length length. Parameter length is optional and defaults to the length of str.

  • Format:

  1. SubStr(str string, start int, length ...int) (substr string)
  • Example:
  1. func ExampleSubStr() {
  2. var (
  3. str = `1234567890`
  4. start = 0
  5. length = 4
  6. subStr = gstr.SubStr(str, start, length)
  7. )
  8. fmt.Println(subStr)
  9. // Output:
  10. // 1234
  11. }

SubStrRune

  • Description: SubStrRune returns a new substring from unicode string str starting at start with length length. Parameter length is optional and defaults to the length of str.

  • Format:

  1. SubStrRune(str string, start int, length ...int) (substr string)
  • Example:
  1. func ExampleSubStrRune() {
  2. var (
  3. str = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架。`
  4. start = 14
  5. length = 3
  6. subStr = gstr.SubStrRune(str, start, length)
  7. )
  8. fmt.Println(subStr)
  9. // Output:
  10. // 高性能
  11. }

StrLimit

  • Description: StrLimit takes a substring from the beginning of str with length length, adds suffix..., and returns the new string.

  • Format:

  1. StrLimit(str string, length int, suffix ...string) string
  • Example:
  1. func ExampleStrLimit() {
  2. var (
  3. str = `123456789`
  4. length = 3
  5. suffix = `...`
  6. result = gstr.StrLimit(str, length, suffix)
  7. )
  8. fmt.Println(result)
  9. // Output:
  10. // 123...
  11. }

StrLimitRune

  • Description: StrLimitRune takes a substring from the beginning of unicode string str with length length, adds suffix..., and returns the new string.

  • Format:

  1. StrLimitRune(str string, length int, suffix ...string) string
  • Example:
  1. func ExampleStrLimitRune() {
  2. var (
  3. str = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架。`
  4. length = 17
  5. suffix = "..."
  6. result = gstr.StrLimitRune(str, length, suffix)
  7. )
  8. fmt.Println(result)
  9. // Output:
  10. // GoFrame是一款模块化、高性能...
  11. }

SubStrFrom

  • Description: SubStrFrom returns the substring from the first occurrence of need to the end of str (including need).

  • Format:

  1. SubStrFrom(str string, need string) (substr string)
  • Example:
  1. func ExampleSubStrFrom() {
  2. var (
  3. str = "我爱GoFrameGood"
  4. need = `爱`
  5. )
  6. fmt.Println(gstr.SubStrFrom(str, need))
  7. // Output:
  8. // 爱GoFrameGood
  9. }

SubStrFromEx

  • Description: SubStrFromEx returns the substring from the first occurrence of need to the end of str (excluding need).

  • Format:

  1. SubStrFromEx(str string, need string) (substr string)
  • Example:
  1. func ExampleSubStrFromEx() {
  2. var (
  3. str = "我爱GoFrameGood"
  4. need = `爱`
  5. )
  6. fmt.Println(gstr.SubStrFromEx(str, need))
  7. // Output:
  8. // GoFrameGood
  9. }

SubStrFromR

  • Description: SubStrFromR returns the substring from the last occurrence of need to the end of str (including need).

  • Format:

  1. SubStrFromR(str string, need string) (substr string)
  • Example:
  1. func ExampleSubStrFromR() {
  2. var (
  3. str = "我爱GoFrameGood"
  4. need = `Go`
  5. )
  6. fmt.Println(gstr.SubStrFromR(str, need))
  7. // Output:
  8. // Good
  9. }

SubStrFromREx

  • Description: SubStrFromREx returns the substring from the last occurrence of need to the end of str (excluding need).

  • Format:

  1. SubStrFromREx(str string, need string) (substr string)
  • Example:
  1. func ExampleSubStrFromREx() {
  2. var (
  3. str = "我爱GoFrameGood"
  4. need = `Go`
  5. )
  6. fmt.Println(gstr.SubStrFromREx(str, need))
  7. // Output:
  8. // od
  9. }

Character/Substring Filtering

Trim

  • Description: Trim removes spaces (or other characters) from the beginning and end of a string. The optional parameter characterMask specifies additional characters to trim.

  • Format:

  1. Trim(str string, characterMask ...string) string
  • Example:
  1. func ExampleTrim() {
  2. var (
  3. str = `*Hello World*`
  4. characterMask = "*d"
  5. result = gstr.Trim(str, characterMask)
  6. )
  7. fmt.Println(result)
  8. // Output:
  9. // Hello Worl
  10. }

TrimStr

  • Description: TrimStr removes all cut strings from the beginning and end of a string (does not remove leading or trailing whitespace).

  • Format:

  1. TrimStr(str string, cut string, count ...int) string
  • Example:
  1. func ExampleTrimStr() {
  2. var (
  3. str = `Hello World`
  4. cut = "World"
  5. count = -1
  6. result = gstr.TrimStr(str, cut, count)
  7. )
  8. fmt.Println(result)
  9. // Output:
  10. // Hello
  11. }

TrimLeft

  • Description: TrimLeft removes spaces (or other characters) from the beginning of a string.

  • Format:

  1. TrimLeft(str string, characterMask ...string) string
  • Example:
  1. func ExampleTrimLeft() {
  2. var (
  3. str = `*Hello World*`
  4. characterMask = "*"
  5. result = gstr.TrimLeft(str, characterMask)
  6. )
  7. fmt.Println(result)
  8. // Output:
  9. // Hello World*
  10. }

TrimLeftStr

  • Description: TrimLeftStr removes count occurrences of the cut string from the beginning of a string (does not remove leading whitespace).

  • Format:

  1. TrimLeftStr(str string, cut string, count ...int) string
  • Example:
  1. func ExampleTrimLeftStr() {
  2. var (
  3. str = `**Hello World**`
  4. cut = "*"
  5. count = 1
  6. result = gstr.TrimLeftStr(str, cut, count)
  7. )
  8. fmt.Println(result)
  9. // Output:
  10. // *Hello World**
  11. }

TrimRight

  • Description: TrimRight removes spaces (or other characters) from the end of a string.

  • Format:

  1. TrimRight(str string, characterMask ...string) string
  • Example:
  1. func ExampleTrimRight() {
  2. var (
  3. str = `**Hello World**`
  4. characterMask = "*def" // []byte{"*", "d", "e", "f"}
  5. result = gstr.TrimRight(str, characterMask)
  6. )
  7. fmt.Println(result)
  8. // Output:
  9. // **Hello Worl
  10. }

TrimRightStr

  • Description: TrimRightStr removes count occurrences of the cut string from the end of a string (does not remove trailing whitespace).

  • Format:

  1. TrimRightStr(str string, cut string, count ...int) string
  • Example:
  1. func ExampleTrimRightStr() {
  2. var (
  3. str = `Hello World!`
  4. cut = "!"
  5. count = -1
  6. result = gstr.TrimRightStr(str, cut, count)
  7. )
  8. fmt.Println(result)
  9. // Output:
  10. // Hello World
  11. }

TrimAll

  • Description: TrimAll removes all spaces (or other characters) and characterMask characters from string str.

  • Format:

  1. TrimAll(str string, characterMask ...string) string
  • Example:
  1. func ExampleTrimAll() {
  2. var (
  3. str = `*Hello World*`
  4. characterMask = "*"
  5. result = gstr.TrimAll(str, characterMask)
  6. )
  7. fmt.Println(result)
  8. // Output:
  9. // HelloWorld
  10. }

HasPrefix

  • Description: HasPrefix returns whether s starts with prefix.

  • Format:

  1. HasPrefix(s, prefix string) bool
  • Example:
  1. func ExampleHasPrefix() {
  2. var (
  3. s = `Hello World`
  4. prefix = "Hello"
  5. result = gstr.HasPrefix(s, prefix)
  6. )
  7. fmt.Println(result)
  8. // Output:
  9. // true
  10. }

HasSuffix

  • Description: HasSuffix returns whether s ends with suffix.

  • Format:

  1. HasSuffix(s, suffix string) bool
  • Example:
  1. func ExampleHasSuffix() {
  2. var (
  3. s = `my best love is goframe`
  4. prefix = "goframe"
  5. result = gstr.HasSuffix(s, prefix)
  6. )
  7. fmt.Println(result)
  8. // Output:
  9. // true
  10. }

Version Comparison

CompareVersion

  • Description: CompareVersion compares a and b as standard GNU versions.

  • Format:

  1. CompareVersion(a, b string) int
  • Example:
  1. func ExampleCompareVersion() {
  2. fmt.Println(gstr.CompareVersion("v2.11.9", "v2.10.8"))
  3. fmt.Println(gstr.CompareVersion("1.10.8", "1.19.7"))
  4. fmt.Println(gstr.CompareVersion("2.8.beta", "2.8"))
  5. // Output:
  6. // 1
  7. // -1
  8. // 0
  9. }

CompareVersionGo

  • Description: CompareVersionGo compares a and b as standard Golang versions.

  • Format:

  1. CompareVersionGo(a, b string) int
  • Example:
  1. func ExampleCompareVersionGo() {
  2. fmt.Println(gstr.CompareVersionGo("v2.11.9", "v2.10.8"))
  3. fmt.Println(gstr.CompareVersionGo("v4.20.1", "v4.20.1+incompatible"))
  4. fmt.Println(gstr.CompareVersionGo(
  5. "v0.0.2-20180626092158-b2ccc119800e",
  6. "v1.0.1-20190626092158-b2ccc519800e",
  7. ))
  8. // Output:
  9. // 1
  10. // 1
  11. // -1
  12. }

Similarity Calculation

Levenshtein

  • Description: Levenshtein calculates the Levenshtein distance between two strings.

  • Format:

  1. Levenshtein(str1, str2 string, costIns, costRep, costDel int) int
  • Example:
  1. func ExampleLevenshtein() {
  2. var (
  3. str1 = "Hello World"
  4. str2 = "hallo World"
  5. costIns = 1
  6. costRep = 1
  7. costDel = 1
  8. result = gstr.Levenshtein(str1, str2, costIns, costRep, costDel)
  9. )
  10. fmt.Println(result)
  11. // Output:
  12. // 2
  13. }

SimilarText

  • Description: SimilarText calculates the similarity between two strings.

  • Format:

  1. SimilarText(first, second string, percent *float64) int
  • Example:
  1. func ExampleSimilarText() {
  2. var (
  3. first = `AaBbCcDd`
  4. second = `ad`
  5. percent = 0.80
  6. result = gstr.SimilarText(first, second, &percent)
  7. )
  8. fmt.Println(result)
  9. // Output:
  10. // 2
  11. }

Soundex

  • Description: Soundex is used to calculate the Soundex key for a string.

  • Format:

  1. Soundex(str string) string
  • Example:
  1. func ExampleSoundex() {
  2. var (
  3. str1 = `Hello`
  4. str2 = `Hallo`
  5. result1 = gstr.Soundex(str1)
  6. result2 = gstr.Soundex(str2)
  7. )
  8. fmt.Println(result1, result2)
  9. // Output:
  10. // H400 H400
  11. }