strutil

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2023 License: MIT Imports: 3 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 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 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 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 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 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: Todo

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

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 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