Documentation ¶
Overview ¶
Package strutil implements some functions to manipulate string.
Index ¶
- func After(s, char string) string
- func AfterLast(s, char string) string
- func ArrayToString(array []interface{}) string
- func Before(s, char string) string
- func BeforeLast(s, char string) string
- func CamelCase(s string) string
- func Capitalize(s string) string
- func FilterPrefix(strs []string, s string) (r []string)
- func FindLongestStr(strs []string) string
- func IsString(v any) bool
- func KebabCase(s string) string
- func LowerFirst(s string) string
- func Pad(source string, size int, padStr string) string
- func PadEnd(source string, size int, padStr string) string
- func PadStart(source string, size int, padStr string) string
- func RemoveNonPrintable(str string) string
- func Reverse(s string) string
- func SnakeCase(s string) string
- func SplitEx(s, sep string, removeEmptyString bool) []string
- func SplitWords(s string) []string
- func StringBuild(arrString ...string) string
- func StringBuildSep(sep string, arrString ...string) string
- func StringConvert(oriString string) string
- func StringIsEmpty(str string) bool
- func StringIsLetter(str string) (bool, error)
- func StringIsNotEmpty(str string) bool
- func StringPrefixSupplementZero(str string, offset int) string
- func StringRandSeq(n int) string
- func StringRandSeq16() string
- func StringSingleSpace(res string) string
- func StringSingleValue(res string, value string) string
- func StringTrim(str string) string
- func StringTrimN(str string) string
- func SubString(res string, start, end int) string
- func Substring(s string, offset int, length uint) string
- func ToString(i interface{}) string
- func Unwrap(str string, wrapToken string) string
- func UpperFirst(s string) string
- func UpperKebabCase(s string) string
- func UpperSnakeCase(s string) string
- func WordCount(s string) int
- func Wrap(str string, wrapWith string) string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func After ¶
After returns the substring after the first occurrence of a specified string in the source string. Play: https://go.dev/play/p/RbCOQqCDA7m
Example ¶
result1 := After("foo", "") result2 := After("foo", "foo") result3 := After("foo/bar", "foo") result4 := After("foo/bar", "/") result5 := 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
func AfterLast ¶
AfterLast returns the substring after the last occurrence of a specified string in the source string. Play: https://go.dev/play/p/1TegARrb8Yn
Example ¶
result1 := AfterLast("foo", "") result2 := AfterLast("foo", "foo") result3 := AfterLast("foo/bar", "/") result4 := AfterLast("foo/bar/baz", "/") result5 := 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
func Before ¶
Before returns the substring of the source string up to the first occurrence of the specified string. Play: https://go.dev/play/p/JAWTZDS4F5w
Example ¶
result1 := Before("foo", "") result2 := Before("foo", "foo") result3 := Before("foo/bar", "/") result4 := Before("foo/bar/baz", "/") fmt.Println(result1) fmt.Println(result2) fmt.Println(result3) fmt.Println(result4)
Output: foo foo foo
func BeforeLast ¶
BeforeLast returns the substring of the source string up to the last occurrence of the specified string. Play: https://go.dev/play/p/pJfXXAoG_Te
Example ¶
result1 := BeforeLast("foo", "") result2 := BeforeLast("foo", "foo") result3 := BeforeLast("foo/bar", "/") result4 := BeforeLast("foo/bar/baz", "/") fmt.Println(result1) fmt.Println(result2) fmt.Println(result3) fmt.Println(result4)
Output: foo foo foo/bar
func CamelCase ¶
CamelCase coverts string to camelCase string. Non letters and numbers will be ignored. Play: https://go.dev/play/p/9eXP3tn2tUy
Example ¶
strings := []string{"", "foobar", "&FOO:BAR$BAZ", "$foo%", "Foo-#1😄$_%^&*(1bar"} for _, v := range strings { s := CamelCase(v) fmt.Println(s) }
Output: foobar fooBarBaz foo foo11Bar
func Capitalize ¶
Capitalize converts the first character of a string to upper case and the remaining to lower case. Play: https://go.dev/play/p/2OAjgbmAqHZ
Example ¶
strings := []string{"", "Foo", "_foo", "fooBar", "foo-bar"} for _, v := range strings { s := Capitalize(v) fmt.Println(s) }
Output: Foo _foo Foobar Foo-bar
func FilterPrefix ¶
FilterPrefix 根据前缀过滤slice
func IsString ¶
IsString check if the value data type is string or not. Play: https://go.dev/play/p/IOgq7oF9ERm
Example ¶
result1 := IsString("") result2 := IsString("a") result3 := IsString(1) result4 := IsString(true) result5 := IsString([]string{"a"}) fmt.Println(result1) fmt.Println(result2) fmt.Println(result3) fmt.Println(result4) fmt.Println(result5)
Output: true true false false false
func KebabCase ¶
KebabCase coverts string to kebab-case, non letters and numbers will be ignored. Play: https://go.dev/play/p/dcZM9Oahw-Y
Example ¶
strings := []string{"", "foo-bar", "Foo Bar-", "FOOBAR", "Foo-#1😄$_%^&*(1bar"} for _, v := range strings { s := KebabCase(v) fmt.Println(s) }
Output: foo-bar foo-bar foobar foo-1-1-bar
func LowerFirst ¶
LowerFirst converts the first character of string to lower case. Play: https://go.dev/play/p/CbzAyZmtJwL
Example ¶
strings := []string{"", "bar", "BAr", "Bar大"} for _, v := range strings { s := LowerFirst(v) fmt.Println(s) }
Output: bar bAr bar大
func Pad ¶
PadStart pads string on the left and right side if it's shorter than size. Padding characters are truncated if they exceed size. Play: https://go.dev/play/p/NzImQq-VF8q
Example ¶
result1 := Pad("foo", 1, "bar") result2 := Pad("foo", 2, "bar") result3 := Pad("foo", 3, "bar") result4 := Pad("foo", 4, "bar") result5 := Pad("foo", 5, "bar") result6 := Pad("foo", 6, "bar") result7 := 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
func PadEnd ¶
PadEnd pads string on the right side if it's shorter than size. Padding characters are truncated if they exceed size. Play: https://go.dev/play/p/9xP8rN0vz--
Example ¶
result1 := PadEnd("foo", 1, "bar") result2 := PadEnd("foo", 2, "bar") result3 := PadEnd("foo", 3, "bar") result4 := PadEnd("foo", 4, "bar") result5 := PadEnd("foo", 5, "bar") result6 := PadEnd("foo", 6, "bar") result7 := 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
func PadStart ¶
PadStart pads string on the left side if it's shorter than size. Padding characters are truncated if they exceed size. Play: https://go.dev/play/p/xpTfzArDfvT
Example ¶
result1 := PadStart("foo", 1, "bar") result2 := PadStart("foo", 2, "bar") result3 := PadStart("foo", 3, "bar") result4 := PadStart("foo", 4, "bar") result5 := PadStart("foo", 5, "bar") result6 := PadStart("foo", 6, "bar") result7 := 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
func RemoveNonPrintable ¶
RemoveNonPrintable remove non-printable characters from a string. Play: https://go.dev/play/p/og47F5x_jTZ
Example ¶
result1 := RemoveNonPrintable("hello\u00a0 \u200bworld\n") result2 := RemoveNonPrintable("你好😄") fmt.Println(result1) fmt.Println(result2)
Output: hello world 你好😄
func Reverse ¶
Reverse returns string whose char order is reversed to the given string. Play: https://go.dev/play/p/adfwalJiecD
Example ¶
s := "foo" rs := Reverse(s) fmt.Println(s) fmt.Println(rs)
Output: foo oof
func SnakeCase ¶
SnakeCase coverts string to snake_case, non letters and numbers will be ignored Play: https://go.dev/play/p/tgzQG11qBuN
Example ¶
strings := []string{"", "foo-bar", "Foo Bar-", "FOOBAR", "Foo-#1😄$_%^&*(1bar"} for _, v := range strings { s := SnakeCase(v) fmt.Println(s) }
Output: foo_bar foo_bar foobar foo_1_1_bar
func SplitEx ¶
SplitEx split a given string which can control the result slice contains empty string or not. Play: https://go.dev/play/p/Us-ySSbWh-3
Example ¶
result1 := SplitEx(" a b c ", "", true) result2 := SplitEx(" a b c ", " ", false) result3 := SplitEx(" a b c ", " ", true) result4 := SplitEx("a = b = c = ", " = ", false) result5 := 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 ] [a b c]
func SplitWords ¶
SplitWords splits a string into words, word only contains alphabetic characters. Play: https://go.dev/play/p/KLiX4WiysMM
Example ¶
result1 := SplitWords("a word") result2 := SplitWords("I'am a programmer") result3 := SplitWords("Bonjour, je suis programmeur") result4 := SplitWords("a -b-c' 'd'e") result5 := SplitWords("你好,我是一名码农") result6 := 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] [] []
func StringBuildSep ¶
StringBuildSep 拼接字符串
func StringConvert ¶
StringConvert 下划线转换,首字母小写变大写, 下划线去掉并将下划线后的首字母大写
func StringPrefixSupplementZero ¶
StringPrefixSupplementZero 当字符串长度不满足时,将字符串前几位补充0
func StringSingleSpace ¶
StringSingleSpace 将字符串内所有连续空格替换为单个空格
func StringSingleValue ¶
StringSingleValue 将字符串内所有连续value替换为单个value
func Substring ¶
Substring returns a substring of the specified length starting at the specified offset position. Play: https://go.dev/play/p/q3sM6ehnPDp
Example ¶
result1 := Substring("abcde", 1, 3) result2 := Substring("abcde", 1, 5) result3 := Substring("abcde", -1, 3) result4 := Substring("abcde", -2, 2) result5 := Substring("abcde", -2, 3) result6 := 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 你好
func Unwrap ¶
Unwrap a given string from anther string. will change source string. Play: https://go.dev/play/p/Ec2q4BzCpG-
Example ¶
result1 := Unwrap("foo", "") result2 := Unwrap("*foo*", "*") result3 := Unwrap("*foo", "*") result4 := Unwrap("foo*", "*") result5 := Unwrap("**foo**", "*") fmt.Println(result1) fmt.Println(result2) fmt.Println(result3) fmt.Println(result4) fmt.Println(result5)
Output: foo foo *foo foo* *foo*
func UpperFirst ¶
UpperFirst converts the first character of string to upper case. Play: https://go.dev/play/p/sBbBxRbs8MM
Example ¶
strings := []string{"", "bar", "BAr", "bar大"} for _, v := range strings { s := UpperFirst(v) fmt.Println(s) }
Output: Bar BAr Bar大
func UpperKebabCase ¶
UpperKebabCase coverts string to upper KEBAB-CASE, non letters and numbers will be ignored Play: https://go.dev/play/p/zDyKNneyQXk
Example ¶
strings := []string{"", "foo-bar", "Foo Bar-", "FooBAR", "Foo-#1😄$_%^&*(1bar"} for _, v := range strings { s := UpperKebabCase(v) fmt.Println(s) }
Output: FOO-BAR FOO-BAR FOO-BAR FOO-1-1-BAR
func UpperSnakeCase ¶
UpperSnakeCase coverts string to upper SNAKE_CASE, non letters and numbers will be ignored Play: https://go.dev/play/p/4COPHpnLx38
Example ¶
strings := []string{"", "foo-bar", "Foo Bar-", "FooBAR", "Foo-#1😄$_%^&*(1bar"} for _, v := range strings { s := UpperSnakeCase(v) fmt.Println(s) }
Output: FOO_BAR FOO_BAR FOO_BAR FOO_1_1_BAR
func WordCount ¶
WordCount return the number of meaningful word, word only contains alphabetic characters. Play: https://go.dev/play/p/bj7_odx3vRf
Example ¶
result1 := WordCount("a word") result2 := WordCount("I'am a programmer") result3 := WordCount("Bonjour, je suis programmeur") result4 := WordCount("a -b-c' 'd'e") result5 := WordCount("你好,我是一名码农") result6 := 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
func Wrap ¶
Wrap a string with given string. Play: https://go.dev/play/p/KoZOlZDDt9y
Example ¶
result1 := Wrap("foo", "") result2 := Wrap("foo", "*") result3 := Wrap("'foo'", "'") result4 := Wrap("", "*") fmt.Println(result1) fmt.Println(result2) fmt.Println(result3) fmt.Println(result4)
Output: foo *foo* ''foo''
Types ¶
This section is empty.