# Strutil
strutil 包含处理字符串的相关函数。
# 源码:
# 用法:
import (
"github.com/duke-git/lancet/v2/strutil"
)
# Documentation 文档
# After
截取源字符串中char首次出现时的位置之后的子字符串
函数签名:
func After(s, char string) string
例子:
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
截取源字符串中char最后一次出现时的位置之后的子字符串
函数签名:
func AfterLast(s, char string) string
例子:
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) // /lancet
}
# Before
截取源字符串中char首次出现时的位置之前的子字符串
函数签名:
func Before(s, char string) string
例子:
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
截取源字符串中char最后一次出现时的位置之前的子字符串
函数签名:
func BeforeLast(s, char string) string
例子:
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
将字符串转换为驼峰式字符串
函数签名:
func CamelCase(s string) string
例子:
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
将字符串的第一个字符转换为大写
函数签名:
func Capitalize(s string) string
例子:
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
检查值的数据类型是否为字符串
函数签名:
func IsString(v any) bool
例子:
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
将字符串转换为kebab-case
函数签名:
func KebabCase(s string) string
例子:
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
将字符串的第一个字符转换为小写
函数签名:
func LowerFirst(s string) string
例子:
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
将字符串的第一个字符转换为大写
函数签名:
func UpperFirst(s string) string
例子:
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
如果字符串长度短于size,则在右侧填充字符串
函数签名:
func PadEnd(source string, size int, padStr string) string
例子:
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
如果字符串长度短于size,则在左侧填充字符串
函数签名:
func PadStart(source string, size int, padStr string) string
例子:
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
返回字符顺序与给定字符串相反的字符串
函数签名:
func Reverse(s string) string
例子:
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
将字符串转换为snake_case形式
函数签名:
func SnakeCase(s string) string
例子:
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
分割字符串为切片,removeEmptyString参数指定是否去除空字符串
函数签名:
func SplitEx(s, sep string, removeEmptyString bool) []string
例子:
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
用另一个字符串包裹一个字符串
函数签名:
func Wrap(str string, wrapWith string) string
例子:
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'
}
# Unwrap
用另一个字符串解开包裹一个字符串
函数签名:
func Unwrap(str string, wrapToken string) string
例子:
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) //***
}