# Strutil | Lancet
A powerful util function library of Go

Skip to content
On this page

# Strutil ​

Package strutil contains some functions to manipulate string.

## Source: ​

- https://github.com/duke-git/lancet/blob/main/strutil/string.go

## Usage: ​

go

    import (
        "github.com/duke-git/lancet/v2/strutil"
    )

    import (
        "github.com/duke-git/lancet/v2/strutil"
    )

## Index ​

- After
- AfterLast
- Before
- BeforeLast
- CamelCase
- Capitalize
- IsString
- KebabCase
- UpperKebabCase
- LowerFirst
- UpperFirst
- Pad
- PadStart
- PadEnd
- Reverse
- SnakeCase
- UpperSnakeCase
- SplitEx
- Substring
- Wrap
- Unwrap
- SplitWords
- WordCount
- RemoveNonPrintable
- StringToBytes
- BytesToString
- IsBlank
- HasPrefixAny
- HasSuffixAny
- IndexOffset
- ReplaceWithMap
- Trim
- SplitAndTrim
- HideString
- ContainsAll
- ContainsAny
- RemoveWhiteSpace

## Documentation ​

### After ​

Returns the substring after the first occurrence of a specified string in the source string.

Signature:

go

    func After(s, char string) string

    func After(s, char string) string

Example:Run

go

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.After("foo", "")
        result2 := strutil.After("foo", "foo")
        result3 := strutil.After("foo/bar", "foo")
        result4 := strutil.After("foo/bar", "/")
        result5 := strutil.After("foo/bar/baz", "/")

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)
        fmt.Println(result4)
        fmt.Println(result5)

        // Output:
        // foo
        //
        // /bar
        // bar
        // bar/baz
    }

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.After("foo", "")
        result2 := strutil.After("foo", "foo")
        result3 := strutil.After("foo/bar", "foo")
        result4 := strutil.After("foo/bar", "/")
        result5 := strutil.After("foo/bar/baz", "/")

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)
        fmt.Println(result4)
        fmt.Println(result5)

        // Output:
        // foo
        //
        // /bar
        // bar
        // bar/baz
    }

### AfterLast ​

Returns the substring after the last occurrence of a specified string in the source string.

Signature:

go

    func AfterLast(s, char string) string

    func AfterLast(s, char string) string

Example:Run

go

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.AfterLast("foo", "")
        result2 := strutil.AfterLast("foo", "foo")
        result3 := strutil.AfterLast("foo/bar", "/")
        result4 := strutil.AfterLast("foo/bar/baz", "/")
        result5 := strutil.AfterLast("foo/bar/foo/baz", "foo")

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)
        fmt.Println(result4)
        fmt.Println(result5)

        // Output:
        // foo
        //
        // bar
        // baz
        // /baz
    }

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.AfterLast("foo", "")
        result2 := strutil.AfterLast("foo", "foo")
        result3 := strutil.AfterLast("foo/bar", "/")
        result4 := strutil.AfterLast("foo/bar/baz", "/")
        result5 := strutil.AfterLast("foo/bar/foo/baz", "foo")

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)
        fmt.Println(result4)
        fmt.Println(result5)

        // Output:
        // foo
        //
        // bar
        // baz
        // /baz
    }

### Before ​

Returns the substring of the source string up to the first occurrence of the specified string.

Signature:

go

    func Before(s, char string) string

    func Before(s, char string) string

Example:Run

go

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.Before("foo", "")
        result2 := strutil.Before("foo", "foo")
        result3 := strutil.Before("foo/bar", "/")
        result4 := strutil.Before("foo/bar/baz", "/")

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)
        fmt.Println(result4)

        // Output:
        // foo
        //
        // foo
        // foo
    }

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.Before("foo", "")
        result2 := strutil.Before("foo", "foo")
        result3 := strutil.Before("foo/bar", "/")
        result4 := strutil.Before("foo/bar/baz", "/")

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)
        fmt.Println(result4)

        // Output:
        // foo
        //
        // foo
        // foo
    }

### BeforeLast ​

Returns the substring of the source string up to the last occurrence of the specified string.

Signature:

go

    func BeforeLast(s, char string) string

    func BeforeLast(s, char string) string

Example:Run

go

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.BeforeLast("foo", "")
        result2 := strutil.BeforeLast("foo", "foo")
        result3 := strutil.BeforeLast("foo/bar", "/")
        result4 := strutil.BeforeLast("foo/bar/baz", "/")

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)
        fmt.Println(result4)

        // Output:
        // foo
        //
        // foo
        // foo/bar
    }

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.BeforeLast("foo", "")
        result2 := strutil.BeforeLast("foo", "foo")
        result3 := strutil.BeforeLast("foo/bar", "/")
        result4 := strutil.BeforeLast("foo/bar/baz", "/")

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)
        fmt.Println(result4)

        // Output:
        // foo
        //
        // foo
        // foo/bar
    }

### CamelCase ​

Coverts string to camelCase string, non letters and numbers will be ignored.

Signature:

go

    func CamelCase(s string) string

    func CamelCase(s string) string

Example:Run

go

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        strings := []string{"", "foobar", "&FOO:BAR$BAZ", "$foo%", "Foo-#1😄$_%^&*(1bar"}

        for _, v := range strings {
            s := strutil.CamelCase(v)
            fmt.Println(s)
        }

        // Output:
        //
        // foobar
        // fooBarBaz
        // foo
        // foo11Bar
    }

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        strings := []string{"", "foobar", "&FOO:BAR$BAZ", "$foo%", "Foo-#1😄$_%^&*(1bar"}

        for _, v := range strings {
            s := strutil.CamelCase(v)
            fmt.Println(s)
        }

        // Output:
        //
        // foobar
        // fooBarBaz
        // foo
        // foo11Bar
    }

### KebabCase ​

KebabCase covert string to kebab-case, non letters and numbers will be ignored.

Signature:

go

    func KebabCase(s string) string

    func KebabCase(s string) string

Example:Run

go

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        strings := []string{"", "foo-bar", "Foo Bar-", "FOOBAR", "Foo-#1😄$_%^&*(1bar"}

        for _, v := range strings {
            s := strutil.KebabCase(v)
            fmt.Println(s)
        }

        // Output:
        //
        // foo-bar
        // foo-bar
        // foobar
        // foo-1-1-bar
    }

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        strings := []string{"", "foo-bar", "Foo Bar-", "FOOBAR", "Foo-#1😄$_%^&*(1bar"}

        for _, v := range strings {
            s := strutil.KebabCase(v)
            fmt.Println(s)
        }

        // Output:
        //
        // foo-bar
        // foo-bar
        // foobar
        // foo-1-1-bar
    }

### UpperKebabCase ​

UpperKebabCase covert string to upper KEBAB-CASE, non letters and numbers will be ignored.

Signature:

go

    func UpperKebabCase(s string) string

    func UpperKebabCase(s string) string

Example:Run

go

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        strings := []string{"", "foo-bar", "Foo Bar-", "FooBAR", "Foo-#1😄$_%^&*(1bar"}

        for _, v := range strings {
            s := strutil.UpperKebabCase(v)
            fmt.Println(s)
        }

        // Output:
        //
        // FOO-BAR
        // FOO-BAR
        // FOO-BAR
        // FOO-1-1-BAR
    }

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        strings := []string{"", "foo-bar", "Foo Bar-", "FooBAR", "Foo-#1😄$_%^&*(1bar"}

        for _, v := range strings {
            s := strutil.UpperKebabCase(v)
            fmt.Println(s)
        }

        // Output:
        //
        // FOO-BAR
        // FOO-BAR
        // FOO-BAR
        // FOO-1-1-BAR
    }

### Capitalize ​

Convert the first character of a string to upper case.

Signature:

go

    func Capitalize(s string) string

    func Capitalize(s string) string

Example:Run

go

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        strings := []string{"", "Foo", "_foo", "fooBar", "foo-bar"}

        for _, v := range strings {
            s := strutil.Capitalize(v)
            fmt.Println(s)
        }

        // Output:
        //
        // Foo
        // _foo
        // Foobar
        // Foo-bar
    }

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        strings := []string{"", "Foo", "_foo", "fooBar", "foo-bar"}

        for _, v := range strings {
            s := strutil.Capitalize(v)
            fmt.Println(s)
        }

        // Output:
        //
        // Foo
        // _foo
        // Foobar
        // Foo-bar
    }

### IsString ​

Check if the value's data type is string.

Signature:

go

    func IsString(v any) bool

    func IsString(v any) bool

Example:Run

go

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.IsString("")
        result2 := strutil.IsString("a")
        result3 := strutil.IsString(1)
        result4 := strutil.IsString(true)
        result5 := strutil.IsString([]string{"a"})

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)
        fmt.Println(result4)
        fmt.Println(result5)

        // Output:
        // true
        // true
        // false
        // false
        // false
    }

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.IsString("")
        result2 := strutil.IsString("a")
        result3 := strutil.IsString(1)
        result4 := strutil.IsString(true)
        result5 := strutil.IsString([]string{"a"})

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)
        fmt.Println(result4)
        fmt.Println(result5)

        // Output:
        // true
        // true
        // false
        // false
        // false
    }

### LowerFirst ​

Convert the first character of string to lower case.

Signature:

go

    func LowerFirst(s string) string

    func LowerFirst(s string) string

Example:Run

go

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        strings := []string{"", "bar", "BAr", "Bar大"}

        for _, v := range strings {
            s := strutil.LowerFirst(v)
            fmt.Println(s)
        }

        // Output:
        //
        // bar
        // bAr
        // bar大
    }

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        strings := []string{"", "bar", "BAr", "Bar大"}

        for _, v := range strings {
            s := strutil.LowerFirst(v)
            fmt.Println(s)
        }

        // Output:
        //
        // bar
        // bAr
        // bar大
    }

### UpperFirst ​

Convert the first character of string to upper case.

Signature:

go

    func UpperFirst(s string) string

    func UpperFirst(s string) string

Example:Run

go

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        strings := []string{"", "bar", "BAr", "bar大"}

        for _, v := range strings {
            s := strutil.UpperFirst(v)
            fmt.Println(s)
        }

        // Output:
        //
        // Bar
        // BAr
        // Bar大
    }

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        strings := []string{"", "bar", "BAr", "bar大"}

        for _, v := range strings {
            s := strutil.UpperFirst(v)
            fmt.Println(s)
        }

        // Output:
        //
        // Bar
        // BAr
        // Bar大
    }

### Pad ​

Pads string on the left and right side if it's shorter than size.

Signature:

go

    func Pad(source string, size int, padStr string) string

    func Pad(source string, size int, padStr string) string

Example:Run

go

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.Pad("foo", 1, "bar")
        result2 := strutil.Pad("foo", 2, "bar")
        result3 := strutil.Pad("foo", 3, "bar")
        result4 := strutil.Pad("foo", 4, "bar")
        result5 := strutil.Pad("foo", 5, "bar")
        result6 := strutil.Pad("foo", 6, "bar")
        result7 := strutil.Pad("foo", 7, "bar")

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)
        fmt.Println(result4)
        fmt.Println(result5)
        fmt.Println(result6)
        fmt.Println(result7)
        // Output:
        // foo
        // foo
        // foo
        // foob
        // bfoob
        // bfooba
        // bafooba
    }

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.Pad("foo", 1, "bar")
        result2 := strutil.Pad("foo", 2, "bar")
        result3 := strutil.Pad("foo", 3, "bar")
        result4 := strutil.Pad("foo", 4, "bar")
        result5 := strutil.Pad("foo", 5, "bar")
        result6 := strutil.Pad("foo", 6, "bar")
        result7 := strutil.Pad("foo", 7, "bar")

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)
        fmt.Println(result4)
        fmt.Println(result5)
        fmt.Println(result6)
        fmt.Println(result7)
        // Output:
        // foo
        // foo
        // foo
        // foob
        // bfoob
        // bfooba
        // bafooba
    }

### PadEnd ​

Pads string on the right side if it's shorter than size.

Signature:

go

    func PadEnd(source string, size int, padStr string) string

    func PadEnd(source string, size int, padStr string) string

Example:Run

go

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.PadEnd("foo", 1, "bar")
        result2 := strutil.PadEnd("foo", 2, "bar")
        result3 := strutil.PadEnd("foo", 3, "bar")
        result4 := strutil.PadEnd("foo", 4, "bar")
        result5 := strutil.PadEnd("foo", 5, "bar")
        result6 := strutil.PadEnd("foo", 6, "bar")
        result7 := strutil.PadEnd("foo", 7, "bar")

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)
        fmt.Println(result4)
        fmt.Println(result5)
        fmt.Println(result6)
        fmt.Println(result7)

        // Output:
        // foo
        // foo
        // foo
        // foob
        // fooba
        // foobar
        // foobarb
    }

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.PadEnd("foo", 1, "bar")
        result2 := strutil.PadEnd("foo", 2, "bar")
        result3 := strutil.PadEnd("foo", 3, "bar")
        result4 := strutil.PadEnd("foo", 4, "bar")
        result5 := strutil.PadEnd("foo", 5, "bar")
        result6 := strutil.PadEnd("foo", 6, "bar")
        result7 := strutil.PadEnd("foo", 7, "bar")

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)
        fmt.Println(result4)
        fmt.Println(result5)
        fmt.Println(result6)
        fmt.Println(result7)

        // Output:
        // foo
        // foo
        // foo
        // foob
        // fooba
        // foobar
        // foobarb
    }

### PadStart ​

Pads string on the left side if it's shorter than size.

Signature:

go

    func PadStart(source string, size int, padStr string) string

    func PadStart(source string, size int, padStr string) string

Example:Run

go

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.PadStart("foo", 1, "bar")
        result2 := strutil.PadStart("foo", 2, "bar")
        result3 := strutil.PadStart("foo", 3, "bar")
        result4 := strutil.PadStart("foo", 4, "bar")
        result5 := strutil.PadStart("foo", 5, "bar")
        result6 := strutil.PadStart("foo", 6, "bar")
        result7 := strutil.PadStart("foo", 7, "bar")

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)
        fmt.Println(result4)
        fmt.Println(result5)
        fmt.Println(result6)
        fmt.Println(result7)

        // Output:
        // foo
        // foo
        // foo
        // bfoo
        // bafoo
        // barfoo
        // barbfoo
    }

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.PadStart("foo", 1, "bar")
        result2 := strutil.PadStart("foo", 2, "bar")
        result3 := strutil.PadStart("foo", 3, "bar")
        result4 := strutil.PadStart("foo", 4, "bar")
        result5 := strutil.PadStart("foo", 5, "bar")
        result6 := strutil.PadStart("foo", 6, "bar")
        result7 := strutil.PadStart("foo", 7, "bar")

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)
        fmt.Println(result4)
        fmt.Println(result5)
        fmt.Println(result6)
        fmt.Println(result7)

        // Output:
        // foo
        // foo
        // foo
        // bfoo
        // bafoo
        // barfoo
        // barbfoo
    }

### Reverse ​

Return string whose char order is reversed to the given string.

Signature:

go

    func Reverse(s string) string

    func Reverse(s string) string

Example:Run

go

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        s := "foo"
        rs := strutil.Reverse(s)

        fmt.Println(s)
        fmt.Println(rs)

        // Output:
        // foo
        // oof
    }

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        s := "foo"
        rs := strutil.Reverse(s)

        fmt.Println(s)
        fmt.Println(rs)

        // Output:
        // foo
        // oof
    }

### SnakeCase ​

Coverts string to snake_case, non letters and numbers will be ignored.

Signature:

go

    func SnakeCase(s string) string

    func SnakeCase(s string) string

Example:Run

go

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        strings := []string{"", "foo-bar", "Foo Bar-", "FOOBAR", "Foo-#1😄$_%^&*(1bar"}

        for _, v := range strings {
            s := strutil.SnakeCase(v)
            fmt.Println(s)
        }

        // Output:
        //
        // foo_bar
        // foo_bar
        // foobar
        // foo_1_1_bar
    }

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        strings := []string{"", "foo-bar", "Foo Bar-", "FOOBAR", "Foo-#1😄$_%^&*(1bar"}

        for _, v := range strings {
            s := strutil.SnakeCase(v)
            fmt.Println(s)
        }

        // Output:
        //
        // foo_bar
        // foo_bar
        // foobar
        // foo_1_1_bar
    }

### UpperSnakeCase ​

Coverts string to upper KEBAB-CASE, non letters and numbers will be ignored.

Signature:

go

    func UpperSnakeCase(s string) string

    func UpperSnakeCase(s string) string

Example:Run

go

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        strings := []string{"", "foo-bar", "Foo Bar-", "FooBAR", "Foo-#1😄$_%^&*(1bar"}

        for _, v := range strings {
            s := strutil.UpperSnakeCase(v)
            fmt.Println(s)
        }

        // Output:
        //
        // FOO_BAR
        // FOO_BAR
        // FOO_BAR
        // FOO_1_1_BAR
    }

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        strings := []string{"", "foo-bar", "Foo Bar-", "FooBAR", "Foo-#1😄$_%^&*(1bar"}

        for _, v := range strings {
            s := strutil.UpperSnakeCase(v)
            fmt.Println(s)
        }

        // Output:
        //
        // FOO_BAR
        // FOO_BAR
        // FOO_BAR
        // FOO_1_1_BAR
    }

### SplitEx ​

Split a given string whether the result contains empty string.

Signature:

go

    func SplitEx(s, sep string, removeEmptyString bool) []string

    func SplitEx(s, sep string, removeEmptyString bool) []string

Example:Run

go

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.SplitEx(" a b c ", "", true)

        result2 := strutil.SplitEx(" a b c ", " ", false)
        result3 := strutil.SplitEx(" a b c ", " ", true)

        result4 := strutil.SplitEx("a = b = c = ", " = ", false)
        result5 := strutil.SplitEx("a = b = c = ", " = ", true)

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)
        fmt.Println(result4)
        fmt.Println(result5)

        // Output:
        // []
        // [ a b c ]
        // [a b c]
        // [a b c ]
    }

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.SplitEx(" a b c ", "", true)

        result2 := strutil.SplitEx(" a b c ", " ", false)
        result3 := strutil.SplitEx(" a b c ", " ", true)

        result4 := strutil.SplitEx("a = b = c = ", " = ", false)
        result5 := strutil.SplitEx("a = b = c = ", " = ", true)

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)
        fmt.Println(result4)
        fmt.Println(result5)

        // Output:
        // []
        // [ a b c ]
        // [a b c]
        // [a b c ]
    }

### Substring ​

Returns a substring of the specified length starting at the specified offset position.

Signature:

go

    func Substring(s string, offset int, length uint) string

    func Substring(s string, offset int, length uint) string

Example:Run

go

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.Substring("abcde", 1, 3)
        result2 := strutil.Substring("abcde", 1, 5)
        result3 := strutil.Substring("abcde", -1, 3)
        result4 := strutil.Substring("abcde", -2, 2)
        result5 := strutil.Substring("abcde", -2, 3)
        result6 := strutil.Substring("你好，欢迎你", 0, 2)

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)
        fmt.Println(result4)
        fmt.Println(result5)
        fmt.Println(result6)

        // Output:
        // bcd
        // bcde
        // e
        // de
        // de
        // 你好
    }

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.Substring("abcde", 1, 3)
        result2 := strutil.Substring("abcde", 1, 5)
        result3 := strutil.Substring("abcde", -1, 3)
        result4 := strutil.Substring("abcde", -2, 2)
        result5 := strutil.Substring("abcde", -2, 3)
        result6 := strutil.Substring("你好，欢迎你", 0, 2)

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)
        fmt.Println(result4)
        fmt.Println(result5)
        fmt.Println(result6)

        // Output:
        // bcd
        // bcde
        // e
        // de
        // de
        // 你好
    }

### Wrap ​

Wrap a string with given string.

Signature:

go

    func Wrap(str string, wrapWith string) string

    func Wrap(str string, wrapWith string) string

Example:Run

go

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.Wrap("foo", "")
        result2 := strutil.Wrap("foo", "*")
        result3 := strutil.Wrap("'foo'", "'")
        result4 := strutil.Wrap("", "*")

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)
        fmt.Println(result4)

        // Output:
        // foo
        // *foo*
        // ''foo''
        //
    }

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.Wrap("foo", "")
        result2 := strutil.Wrap("foo", "*")
        result3 := strutil.Wrap("'foo'", "'")
        result4 := strutil.Wrap("", "*")

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)
        fmt.Println(result4)

        // Output:
        // foo
        // *foo*
        // ''foo''
        //
    }

### Unwrap ​

Unwrap a given string from anther string. will change source string.

Signature:

go

    func Unwrap(str string, wrapToken string) string

    func Unwrap(str string, wrapToken string) string

Example:Run

go

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.Unwrap("foo", "")
        result2 := strutil.Unwrap("*foo*", "*")
        result3 := strutil.Unwrap("*foo", "*")
        result4 := strutil.Unwrap("foo*", "*")
        result5 := strutil.Unwrap("**foo**", "*")

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)
        fmt.Println(result4)
        fmt.Println(result5)

        // Output:
        // foo
        // foo
        // *foo
        // foo*
        // *foo*
    }

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.Unwrap("foo", "")
        result2 := strutil.Unwrap("*foo*", "*")
        result3 := strutil.Unwrap("*foo", "*")
        result4 := strutil.Unwrap("foo*", "*")
        result5 := strutil.Unwrap("**foo**", "*")

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)
        fmt.Println(result4)
        fmt.Println(result5)

        // Output:
        // foo
        // foo
        // *foo
        // foo*
        // *foo*
    }

### SplitWords ​

Splits a string into words, word only contains alphabetic characters.

Signature:

go

    func SplitWords(s string) []string

    func SplitWords(s string) []string

Example:Run

go

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.SplitWords("a word")
        result2 := strutil.SplitWords("I'am a programmer")
        result3 := strutil.SplitWords("Bonjour, je suis programmeur")
        result4 := strutil.SplitWords("a -b-c' 'd'e")
        result5 := strutil.SplitWords("你好，我是一名码农")
        result6 := strutil.SplitWords("こんにちは，私はプログラマーです")

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)
        fmt.Println(result4)
        fmt.Println(result5)
        fmt.Println(result6)

        // Output:
        // [a word]
        // [I'am a programmer]
        // [Bonjour je suis programmeur]
        // [a b-c' d'e]
        // []
        // []
    }

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.SplitWords("a word")
        result2 := strutil.SplitWords("I'am a programmer")
        result3 := strutil.SplitWords("Bonjour, je suis programmeur")
        result4 := strutil.SplitWords("a -b-c' 'd'e")
        result5 := strutil.SplitWords("你好，我是一名码农")
        result6 := strutil.SplitWords("こんにちは，私はプログラマーです")

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)
        fmt.Println(result4)
        fmt.Println(result5)
        fmt.Println(result6)

        // Output:
        // [a word]
        // [I'am a programmer]
        // [Bonjour je suis programmeur]
        // [a b-c' d'e]
        // []
        // []
    }

### WordCount ​

Return the number of meaningful word, word only contains alphabetic characters.

Signature:

go

    func WordCount(s string) int

    func WordCount(s string) int

Example:Run

go

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.WordCount("a word")
        result2 := strutil.WordCount("I'am a programmer")
        result3 := strutil.WordCount("Bonjour, je suis programmeur")
        result4 := strutil.WordCount("a -b-c' 'd'e")
        result5 := strutil.WordCount("你好，我是一名码农")
        result6 := strutil.WordCount("こんにちは，私はプログラマーです")

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)
        fmt.Println(result4)
        fmt.Println(result5)
        fmt.Println(result6)

        // Output:
        // 2
        // 3
        // 4
        // 3
        // 0
        // 0
    }

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.WordCount("a word")
        result2 := strutil.WordCount("I'am a programmer")
        result3 := strutil.WordCount("Bonjour, je suis programmeur")
        result4 := strutil.WordCount("a -b-c' 'd'e")
        result5 := strutil.WordCount("你好，我是一名码农")
        result6 := strutil.WordCount("こんにちは，私はプログラマーです")

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)
        fmt.Println(result4)
        fmt.Println(result5)
        fmt.Println(result6)

        // Output:
        // 2
        // 3
        // 4
        // 3
        // 0
        // 0
    }

### RemoveNonPrintable ​

Remove non-printable characters from a string.

Signature:

go

    func RemoveNonPrintable(str string) string

    func RemoveNonPrintable(str string) string

Example:Run

go

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.RemoveNonPrintable("hello\u00a0 \u200bworld\n")
        result2 := strutil.RemoveNonPrintable("你好😄")

        fmt.Println(result1)
        fmt.Println(result2)

        // Output:
        // hello world
        // 你好😄
    }

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.RemoveNonPrintable("hello\u00a0 \u200bworld\n")
        result2 := strutil.RemoveNonPrintable("你好😄")

        fmt.Println(result1)
        fmt.Println(result2)

        // Output:
        // hello world
        // 你好😄
    }

### StringToBytes ​

Converts a string to byte slice without a memory allocation.

Signature:

go

    func StringToBytes(str string) (b []byte)

    func StringToBytes(str string) (b []byte)

Example:Run

go

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.StringToBytes("abc")
        result2 := reflect.DeepEqual(result1, []byte{'a', 'b', 'c'})

        fmt.Println(result1)
        fmt.Println(result2)

        // Output:
        // [97 98 99]
        // true
    }

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.StringToBytes("abc")
        result2 := reflect.DeepEqual(result1, []byte{'a', 'b', 'c'})

        fmt.Println(result1)
        fmt.Println(result2)

        // Output:
        // [97 98 99]
        // true
    }

### BytesToString ​

Converts a byte slice to string without a memory allocation.

Signature:

go

    func BytesToString(bytes []byte) string

    func BytesToString(bytes []byte) string

Example:Run

go

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        bytes := []byte{'a', 'b', 'c'}
        result := strutil.BytesToString(bytes)

        fmt.Println(result)

        // Output:
        // abc
    }

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        bytes := []byte{'a', 'b', 'c'}
        result := strutil.BytesToString(bytes)

        fmt.Println(result)

        // Output:
        // abc
    }

### IsBlank ​

Checks if a string is whitespace or empty.

Signature:

go

    func IsBlank(str string) bool

    func IsBlank(str string) bool

Example:Run

go

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.IsBlank("")
        result2 := strutil.IsBlank("\t\v\f\n")
        result3 := strutil.IsBlank(" 中文")

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)

        // Output:
        // true
        // true
        // false
    }

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.IsBlank("")
        result2 := strutil.IsBlank("\t\v\f\n")
        result3 := strutil.IsBlank(" 中文")

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)

        // Output:
        // true
        // true
        // false
    }

### HasPrefixAny ​

Checks if a string starts with any of an array of specified strings.

Signature:

go

    func HasPrefixAny(str string, prefixes []string) bool

    func HasPrefixAny(str string, prefixes []string) bool

Example:Run

go

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.HasPrefixAny("foo bar", []string{"fo", "xyz", "hello"})
        result2 := strutil.HasPrefixAny("foo bar", []string{"oom", "world"})

        fmt.Println(result1)
        fmt.Println(result2)

        // Output:
        // true
        // false
    }

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.HasPrefixAny("foo bar", []string{"fo", "xyz", "hello"})
        result2 := strutil.HasPrefixAny("foo bar", []string{"oom", "world"})

        fmt.Println(result1)
        fmt.Println(result2)

        // Output:
        // true
        // false
    }

### HasSuffixAny ​

Checks if a string ends with any of an array of specified strings.

Signature:

go

    func HasSuffixAny(str string, suffixes []string) bool

    func HasSuffixAny(str string, suffixes []string) bool

Example:Run

go

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.HasSuffixAny("foo bar", []string{"bar", "xyz", "hello"})
        result2 := strutil.HasSuffixAny("foo bar", []string{"oom", "world"})

        fmt.Println(result1)
        fmt.Println(result2)

        // Output:
        // true
        // false
    }

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.HasSuffixAny("foo bar", []string{"bar", "xyz", "hello"})
        result2 := strutil.HasSuffixAny("foo bar", []string{"oom", "world"})

        fmt.Println(result1)
        fmt.Println(result2)

        // Output:
        // true
        // false
    }

### IndexOffset ​

Returns the index of the first instance of substr in string after offsetting the string by `idxFrom`, or -1 if substr is not present in string.

Signature:

go

    func IndexOffset(str string, substr string, idxFrom int) int

    func IndexOffset(str string, substr string, idxFrom int) int

Example:Run

go

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        str := "foo bar hello world"

        result1 := strutil.IndexOffset(str, "o", 5)
        result2 := strutil.IndexOffset(str, "o", 0)
        result3 := strutil.IndexOffset(str, "d", len(str)-1)
        result4 := strutil.IndexOffset(str, "d", len(str))
        result5 := strutil.IndexOffset(str, "f", -1)

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)
        fmt.Println(result4)
        fmt.Println(result5)

        // Output:
        // 12
        // 1
        // 18
        // -1
        // -1
    }

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        str := "foo bar hello world"

        result1 := strutil.IndexOffset(str, "o", 5)
        result2 := strutil.IndexOffset(str, "o", 0)
        result3 := strutil.IndexOffset(str, "d", len(str)-1)
        result4 := strutil.IndexOffset(str, "d", len(str))
        result5 := strutil.IndexOffset(str, "f", -1)

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)
        fmt.Println(result4)
        fmt.Println(result5)

        // Output:
        // 12
        // 1
        // 18
        // -1
        // -1
    }

### ReplaceWithMap ​

Returns a copy of `str`, which is replaced by a map in unordered way, case-sensitively.

Signature:

go

    func ReplaceWithMap(str string, replaces map[string]string) string

    func ReplaceWithMap(str string, replaces map[string]string) string

Example:Run

go

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        str := "ac ab ab ac"
        replaces := map[string]string{
            "a": "1",
            "b": "2",
        }

        result := strutil.ReplaceWithMap(str, replaces)

        fmt.Println(result)
        // Output:
        // 1c 12 12 1c
    }

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        str := "ac ab ab ac"
        replaces := map[string]string{
            "a": "1",
            "b": "2",
        }

        result := strutil.ReplaceWithMap(str, replaces)

        fmt.Println(result)
        // Output:
        // 1c 12 12 1c
    }

### Trim ​

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

Signature:

go

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

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

Example:Run

go

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.Trim("\nabcd")

        str := "$ ab    cd $ "

        result2 := strutil.Trim(str)
        result3 := strutil.Trim(str, "$")

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)

        // Output:
        // abcd
        // $ ab    cd $
        // ab    cd
    }

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        result1 := strutil.Trim("\nabcd")

        str := "$ ab    cd $ "

        result2 := strutil.Trim(str)
        result3 := strutil.Trim(str, "$")

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)

        // Output:
        // abcd
        // $ ab    cd $
        // ab    cd
    }

### SplitAndTrim ​

Splits string `str` by a string `delimiter` to a slice, and calls Trim to every element of slice. It ignores the elements which are empty after Trim.

Signature:

go

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

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

Example:Run

go

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        str := " a,b, c,d,$1 "

        result1 := strutil.SplitAndTrim(str, ",")
        result2 := strutil.SplitAndTrim(str, ",", "$")

        fmt.Println(result1)
        fmt.Println(result2)

        // Output:
        // [a b c d $1]
        // [a b c d 1]
    }

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        str := " a,b, c,d,$1 "

        result1 := strutil.SplitAndTrim(str, ",")
        result2 := strutil.SplitAndTrim(str, ",", "$")

        fmt.Println(result1)
        fmt.Println(result2)

        // Output:
        // [a b c d $1]
        // [a b c d 1]
    }

### HideString ​

Hide some chars in source string with param `replaceChar`. replace range is origin[start : end]. [start, end).

Signature:

go

    func HideString(origin string, start, end int, replaceChar string) string

    func HideString(origin string, start, end int, replaceChar string) string

Example:Run

go

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        str := "13242658976"

        result1 := strutil.HideString(str, 3, 3, "*")
        result2 := strutil.HideString(str, 3, 4, "*")
        result3 := strutil.HideString(str, 3, 7, "*")
        result4 := strutil.HideString(str, 7, 11, "*")

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)
        fmt.Println(result4)

        // Output:
        // 13242658976
        // 132*2658976
        // 132****8976
        // 1324265****
    }

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        str := "13242658976"

        result1 := strutil.HideString(str, 3, 3, "*")
        result2 := strutil.HideString(str, 3, 4, "*")
        result3 := strutil.HideString(str, 3, 7, "*")
        result4 := strutil.HideString(str, 7, 11, "*")

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)
        fmt.Println(result4)

        // Output:
        // 13242658976
        // 132*2658976
        // 132****8976
        // 1324265****
    }

### ContainsAll ​

Return true if target string contains all the substrings.

Signature:

go

    func ContainsAll(str string, substrs []string) bool

    func ContainsAll(str string, substrs []string) bool

Example:Run

go

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        str := "hello world"

        result1 := strutil.ContainsAll(str, []string{"hello", "world"})
        result2 := strutil.ContainsAll(str, []string{"hello", "abc"})

        fmt.Println(result1)
        fmt.Println(result2)

        // Output:
        // true
        // false
    }

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        str := "hello world"

        result1 := strutil.ContainsAll(str, []string{"hello", "world"})
        result2 := strutil.ContainsAll(str, []string{"hello", "abc"})

        fmt.Println(result1)
        fmt.Println(result2)

        // Output:
        // true
        // false
    }

### ContainsAny ​

Return true if target string contains any one of the substrings.

Signature:

go

    func ContainsAny(str string, substrs []string) bool

    func ContainsAny(str string, substrs []string) bool

Example:Run

go

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        str := "hello world"

        result1 := strutil.ContainsAny(str, []string{"hello", "world"})
        result2 := strutil.ContainsAny(str, []string{"hello", "abc"})
        result3 := strutil.ContainsAny(str, []string{"123", "abc"})

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)

        // Output:
        // true
        // true
        // false
    }

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        str := "hello world"

        result1 := strutil.ContainsAny(str, []string{"hello", "world"})
        result2 := strutil.ContainsAny(str, []string{"hello", "abc"})
        result3 := strutil.ContainsAny(str, []string{"123", "abc"})

        fmt.Println(result1)
        fmt.Println(result2)
        fmt.Println(result3)

        // Output:
        // true
        // true
        // false
    }

### RemoveWhiteSpace ​

Remove whitespace characters from a string. when set repalceAll is true removes all whitespace, false only replaces consecutive whitespace characters with one space.

Signature:

go

    func RemoveWhiteSpace(str string, repalceAll bool) string

    func RemoveWhiteSpace(str string, repalceAll bool) string

Example:Run

go

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        str := " hello   \r\n    \t   world"

        result1 := strutil.RemoveWhiteSpace(str, true)
        result2 := strutil.RemoveWhiteSpace(str, false)

        fmt.Println(result1)
        fmt.Println(result2)

        // Output:
        // helloworld
        // hello world
    }

    import (
        "fmt"
        "github.com/duke-git/lancet/v2/strutil"
    )

    func main() {
        str := " hello   \r\n    \t   world"

        result1 := strutil.RemoveWhiteSpace(str, true)
        result2 := strutil.RemoveWhiteSpace(str, false)

        fmt.Println(result1)
        fmt.Println(result2)

        // Output:
        // helloworld
        // hello world
    }