# Strutil

Package strutil contains some functions to manipulate string.

# Source:

# Usage:

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

# Documentation

# After

Creates substring in source string after position when char first appear.

Signature:

func After(s, char string) string

Example:

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

func main() {
	s1 := strutil.After("lancet", "")
	fmt.Println(s1) //lancet

	s2 := strutil.After("github.com/test/lancet", "/")
	fmt.Println(s2) //test/lancet

	s3 := strutil.After("github.com/test/lancet", "test")
	fmt.Println(s3) // /lancet
}

# AfterLast

Creates substring in source string after position when char last appear.

Signature:

func AfterLast(s, char string) string

Example:

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

func main() {
	s1 := strutil.AfterLast("lancet", "")
	fmt.Println(s1) //lancet

	s2 := strutil.AfterLast("github.com/test/lancet", "/")
	fmt.Println(s2) //lancet

	s3 := strutil.AfterLast("github.com/test/test/lancet", "test")
	fmt.Println(s3) // /test/lancet
}

# Before

Creates substring in source string before position when char first appear.

Signature:

func Before(s, char string) string

Example:

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

func main() {
	s1 := strutil.Before("lancet", "")
	fmt.Println(s1) //lancet

	s2 := strutil.Before("github.com/test/lancet", "/")
	fmt.Println(s2) //github.com

	s3 := strutil.Before("github.com/test/lancet", "test")
	fmt.Println(s3) // github.com/
}

# BeforeLast

Creates substring in source string before position when char first appear.

Signature:

func BeforeLast(s, char string) string

Example:

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

func main() {
	s1 := strutil.BeforeLast("lancet", "")
	fmt.Println(s1) //lancet

	s2 := strutil.BeforeLast("github.com/test/lancet", "/")
	fmt.Println(s2) //github.com/test

	s3 := strutil.BeforeLast("github.com/test/test/lancet", "test")
	fmt.Println(s3) //github.com/test/
}

# CamelCase

Covert string to camelCase string.

Signature:

func CamelCase(s string) string

Example:

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

func main() {
	s1 := strutil.CamelCase("foo_bar")
	fmt.Println(s1) //fooBar

	s2 := strutil.CamelCase("Foo-Bar")
	fmt.Println(s2) //fooBar

	s3 := strutil.CamelCase("Foo&bar")
	fmt.Println(s3) //fooBar

	s4 := strutil.CamelCase("foo bar")
	fmt.Println(s4) //fooBar
}

# Capitalize

Convert the first character of a string to upper case.

Signature:

func Capitalize(s string) string

Example:

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

func main() {
	s1 := strutil.Capitalize("foo")
	fmt.Println(s1) //foo

	s2 := strutil.Capitalize("Foo")
	fmt.Println(s2) //foo

	s3 := strutil.Capitalize("FOo"
	fmt.Println(s3) //fOo
}

# IsString

Check if the value's data type is string.

Signature:

func IsString(v any) bool

Example:

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

func main() {
	fmt.Println(strutil.IsString("lancet")) //true
	fmt.Println(strutil.IsString("")) //true

	fmt.Println(strutil.IsString(1)) //false
	fmt.Println(strutil.IsString("")) //false
	fmt.Println(strutil.IsString([]string{})) //false
}

# KebabCase

Covert string to kebab-case.

Signature:

func KebabCase(s string) string

Example:

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

func main() {
	s1 := strutil.KebabCase("Foo Bar-")
	fmt.Println(s1) //foo-bar

	s2 := strutil.KebabCase("foo_Bar")
	fmt.Println(s2) //foo-bar

	s3 := strutil.KebabCase("fooBar")
	fmt.Println(s3) //foo-bar

	s4 := strutil.KebabCase("__FOO_BAR__")
	fmt.Println(s4) //f-o-o-b-a-r
}

# LowerFirst

Convert the first character of string to lower case.

Signature:

func LowerFirst(s string) string

Example:

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

func main() {
	s1 := strutil.LowerFirst("foo")
	fmt.Println(s1) //foo

	s2 := strutil.LowerFirst("BAR")
	fmt.Println(s2) //bAR

	s3 := strutil.LowerFirst("FOo")
	fmt.Println(s3) //fOo

	s4 := strutil.LowerFirst("fOo大")
	fmt.Println(s4) //fOo大
}

# UpperFirst

Convert the first character of string to upper case.

Signature:

func UpperFirst(s string) string

Example:

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

func main() {
	s1 := strutil.UpperFirst("foo")
	fmt.Println(s1) //Foo

	s2 := strutil.UpperFirst("bAR")
	fmt.Println(s2) //BAR

	s3 := strutil.UpperFirst("FOo")
	fmt.Println(s3) //FOo

	s4 := strutil.UpperFirst("fOo大")
	fmt.Println(s4) //FOo大
}

# PadEnd

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

Signature:

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

Example:

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

func main() {
	s1 := strutil.PadEnd("a", 1, "b")
	fmt.Println(s1) //a

	s2 := strutil.PadEnd("a", 2, "b")
	fmt.Println(s2) //ab

	s3 := strutil.PadEnd("abcd", 6, "mno")
	fmt.Println(s3) //abcdmn

	s4 := strutil.PadEnd("abc", 6, "ab")
	fmt.Println(s4) //abcaba
}

# PadStart

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

Signature:

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

Example:

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

func main() {
	s1 := strutil.PadStart("a", 1, "b")
	fmt.Println(s1) //a

	s2 := strutil.PadStart("a", 2, "b")
	fmt.Println(s2) //ba

	s3 := strutil.PadStart("abcd", 6, "mno")
	fmt.Println(s3) //mnabcd

	s4 := strutil.PadStart("abc", 6, "ab")
	fmt.Println(s4) //abaabc
}

# Reverse

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

Signature:

func Reverse(s string) string

Example:

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

func main() {
	s1 := strutil.ReverseStr("abc")
	fmt.Println(s1) //cba

	s2 := strutil.ReverseStr("12345")
	fmt.Println(s2) //54321
}

# SnakeCase

Covert string to snake_case.

Signature:

func SnakeCase(s string) string

Example:

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

func main() {
	s1 := strutil.SnakeCase("Foo Bar-")
	fmt.Println(s1) //foo_bar

	s2 := strutil.SnakeCase("foo_Bar")
	fmt.Println(s2) //foo_bar

	s3 := strutil.SnakeCase("fooBar")
	fmt.Println(s3) //foo_bar

	s4 := strutil.SnakeCase("__FOO_BAR__")
	fmt.Println(s4) //f_o_o_b_a_r

	s5 := strutil.SnakeCase("aBbc-s$@a&%_B.B^C")
	fmt.Println(s5) //a_bbc_s_a_b_b_c
}

# SplitEx

Split a given string whether the result contains empty string.

Signature:

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

Example:

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

func main() {
	arr1 := strutil.SplitEx(" a b c ", "", true)
	fmt.Println(arr1) //[]string{}

	arr2 := strutil.SplitEx(" a b c ", " ", false)
	fmt.Println(arr2) //[]string{"", "a", "b", "c", ""}

	arr3 := strutil.SplitEx(" a b c ", " ", true)
	fmt.Println(arr3) //[]string{"a", "b", "c"}

	arr4 := strutil.SplitEx(" a = b = c = ", " = ", false)
	fmt.Println(arr4) //[]string{" a", "b", "c", ""}

	arr5 := strutil.SplitEx(" a = b = c = ", " = ", true)
	fmt.Println(arr5) //[]string{" a", "b", "c"}
}

# Wrap

Wrap a string with another string.

Signature:

func Wrap(str string, wrapWith string) string

Example:

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

func main() {
	s1 := strutil.Wrap("ab", "")
	fmt.Println(s1) //ab

	s2 := strutil.Wrap("", "*")
	fmt.Println(s2) //""

	s3 := strutil.Wrap("ab", "*")
	fmt.Println(s3) //*ab*

	s4 := strutil.Wrap("ab", "\"")
	fmt.Println(s4) //\"ab\"

	s5 := strutil.Wrap("ab", "'")
	fmt.Println(s5) //'ab'
}

# Wrap

Unwrap a given string from anther string. will change str value.

Signature:

func Unwrap(str string, wrapToken string) string

Example:

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

func main() {
	s1 := strutil.Unwrap("ab", "")
	fmt.Println(s1) //ab

	s2 := strutil.Unwrap("ab", "*")
	fmt.Println(s2) //ab

	s3 := strutil.Unwrap("**ab**", "*")
	fmt.Println(s3) //*ab*

	s4 := strutil.Unwrap("*ab", "*")
	fmt.Println(s4) //*ab

	s5 := strutil.Unwrap("***", "**")
	fmt.Println(s5) //***
}
Last Updated: 9/29/2022, 1:53:14 PM