strutil

package
v0.0.0-...-d1e1ff0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 11, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package strutil implements some functions to manipulate string.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func After

func After(s, char string) string

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

func AfterLast(s, char string) string

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 ArrayToString

func ArrayToString(array []interface{}) string

ArrayToString 数字切片变字符串

func Before

func Before(s, char string) string

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

func BeforeLast(s, char string) string

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

func CamelCase(s string) string

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

func Capitalize(s string) string

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

func FilterPrefix(strs []string, s string) (r []string)

FilterPrefix 根据前缀过滤slice

func FindLongestStr

func FindLongestStr(strs []string) string

FindLongestStr 查询最长字符串

func IsString

func IsString(v any) bool

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

func KebabCase(s string) string

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

func LowerFirst(s string) string

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

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

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

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

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

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

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

func RemoveNonPrintable(str string) string

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

func Reverse(s string) string

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

func SnakeCase(s string) string

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

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

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

func SplitWords(s string) []string

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 StringBuild

func StringBuild(arrString ...string) string

StringBuild 拼接字符串

func StringBuildSep

func StringBuildSep(sep string, arrString ...string) string

StringBuildSep 拼接字符串

func StringConvert

func StringConvert(oriString string) string

StringConvert 下划线转换,首字母小写变大写, 下划线去掉并将下划线后的首字母大写

func StringIsEmpty

func StringIsEmpty(str string) bool

StringIsEmpty 判断字符串是否为空,是则返回true,否则返回false

func StringIsLetter

func StringIsLetter(str string) (bool, error)

StringAllLetter 判断字符串是否只由字母组成

func StringIsNotEmpty

func StringIsNotEmpty(str string) bool

StringIsNotEmpty 和 IsEmpty 的语义相反

func StringPrefixSupplementZero

func StringPrefixSupplementZero(str string, offset int) string

StringPrefixSupplementZero 当字符串长度不满足时,将字符串前几位补充0

func StringRandSeq

func StringRandSeq(n int) string

StringRandSeq 创建指定长度的随机字符串

func StringRandSeq16

func StringRandSeq16() string

StringRandSeq16 创建长度为16的随机字符串

func StringSingleSpace

func StringSingleSpace(res string) string

StringSingleSpace 将字符串内所有连续空格替换为单个空格

func StringSingleValue

func StringSingleValue(res string, value string) string

StringSingleValue 将字符串内所有连续value替换为单个value

func StringTrim

func StringTrim(str string) string

StringTrim 去除字符串中的空格和换行符

func StringTrimN

func StringTrimN(str string) string

StringTrimN 去除字符串中的换行符

func SubString

func SubString(res string, start, end int) string

SubString 截取字符串

func Substring

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

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 ToString

func ToString(i interface{}) string

ToString 将对象格式化成字符串

func Unwrap

func Unwrap(str string, wrapToken string) string

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

func UpperFirst(s string) string

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

func UpperKebabCase(s string) string

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

func UpperSnakeCase(s string) string

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

func WordCount(s string) int

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

func Wrap(str string, wrapWith string) string

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL