strutil

package
v2.2.5 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2023 License: MIT Imports: 6 Imported by: 36

Documentation

Overview

Package strutil implements some functions to manipulate string.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// DefaultTrimChars are the characters which are stripped by Trim* functions in default.
	DefaultTrimChars = string([]byte{
		'\t',
		'\v',
		'\n',
		'\r',
		'\f',
		' ',
		0x00,
		0x85,
		0xA0,
	})
)

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 BytesToString added in v2.1.20

func BytesToString(bytes []byte) string

BytesToString converts a byte slice to string without a memory allocation. Play: https://go.dev/play/p/6c68HRvJecH

Example
bytes := []byte{'a', 'b', 'c'}
result := BytesToString(bytes)

fmt.Println(result)
Output:

abc

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 ContainsAll added in v2.2.1

func ContainsAll(str string, substrs []string) bool

ContainsAll return true if target string contains all the substrs. Play: https://go.dev/play/p/KECtK2Os4zq

Example
str := "hello world"

result1 := ContainsAll(str, []string{"hello", "world"})
result2 := ContainsAll(str, []string{"hello", "abc"})

fmt.Println(result1)
fmt.Println(result2)
Output:

true
false

func ContainsAny added in v2.2.1

func ContainsAny(str string, substrs []string) bool

ContainsAny return true if target string contains any one of the substrs. Play: https://go.dev/play/p/dZGSSMB3LXE

Example
str := "hello world"

result1 := ContainsAny(str, []string{"hello", "world"})
result2 := ContainsAny(str, []string{"hello", "abc"})
result3 := ContainsAny(str, []string{"123", "abc"})

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
Output:

true
true
false

func HasPrefixAny added in v2.1.20

func HasPrefixAny(str string, prefixes []string) bool

HasPrefixAny check if a string starts with any of a slice of specified strings. Play: https://go.dev/play/p/8UUTl2C5slo

Example
result1 := HasPrefixAny("foo bar", []string{"fo", "xyz", "hello"})
result2 := HasPrefixAny("foo bar", []string{"oom", "world"})

fmt.Println(result1)
fmt.Println(result2)
Output:

true
false

func HasSuffixAny added in v2.1.20

func HasSuffixAny(str string, suffixes []string) bool

HasSuffixAny check if a string ends with any of a slice of specified strings. Play: https://go.dev/play/p/sKWpCQdOVkx

Example
result1 := HasSuffixAny("foo bar", []string{"bar", "xyz", "hello"})
result2 := HasSuffixAny("foo bar", []string{"oom", "world"})

fmt.Println(result1)
fmt.Println(result2)
Output:

true
false

func HideString added in v2.2.1

func HideString(origin string, start, end int, replaceChar string) string

HideString hide some chars in source string with param `replaceChar`. replace range is origin[start : end]. [start, end) Play: https://go.dev/play/p/pzbaIVCTreZ)

Example
str := "13242658976"

result1 := HideString(str, 3, 3, "*")
result2 := HideString(str, 3, 4, "*")
result3 := HideString(str, 3, 7, "*")
result4 := HideString(str, 7, 11, "*")

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
Output:

13242658976
132*2658976
132****8976
1324265****

func IndexOffset added in v2.1.20

func IndexOffset(str string, substr string, idxFrom int) int

IndexOffset returns the index of the first instance of substr in string after offsetting the string by `idxFrom`, or -1 if substr is not present in string. Play: https://go.dev/play/p/qZo4lV2fomB

Example
str := "foo bar hello world"

result1 := IndexOffset(str, "o", 5)
result2 := IndexOffset(str, "o", 0)
result3 := IndexOffset(str, "d", len(str)-1)
result4 := IndexOffset(str, "d", len(str))
result5 := IndexOffset(str, "f", -1)

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
Output:

12
1
18
-1
-1

func IsBlank added in v2.1.20

func IsBlank(str string) bool

IsBlank checks if a string is whitespace, empty. Play: https://go.dev/play/p/6zXRH_c0Qd3

Example
result1 := IsBlank("")
result2 := IsBlank("\t\v\f\n")
result3 := IsBlank(" 中文")

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
Output:

true
true
false

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 added in v2.1.16

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 added in v2.1.18

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 RemoveWhiteSpace added in v2.2.2

func RemoveWhiteSpace(str string, repalceAll bool) string

RemoveWhiteSpace remove whitespace characters from a string. when set repalceAll is true removes all whitespace, false only replaces consecutive whitespace characters with one space. Play: https://go.dev/play/p/HzLC9vsTwkf

Example
str := " hello   \r\n	\t   world"

result1 := RemoveWhiteSpace(str, true)
result2 := RemoveWhiteSpace(str, false)

fmt.Println(result1)
fmt.Println(result2)
Output:

helloworld
hello world

func ReplaceWithMap added in v2.2.0

func ReplaceWithMap(str string, replaces map[string]string) string

ReplaceWithMap returns a copy of `str`, which is replaced by a map in unordered way, case-sensitively. Play: https://go.dev/play/p/h3t7CNj2Vvu

Example
str := "ac ab ab ac"
replaces := map[string]string{
	"a": "1",
	"b": "2",
}

result := ReplaceWithMap(str, replaces)

fmt.Println(result)
Output:

1c 12 12 1c

func Reverse added in v2.1.5

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 SplitAndTrim added in v2.2.0

func SplitAndTrim(str, delimiter string, characterMask ...string) []string

SplitAndTrim splits string `str` by a string `delimiter` to a slice, and calls Trim to every element of this slice. It ignores the elements which are empty after Trim. Play: https://go.dev/play/p/ZNL6o4SkYQ7

Example
str := " a,b, c,d,$1 "

result1 := SplitAndTrim(str, ",")
result2 := SplitAndTrim(str, ",", "$")

fmt.Println(result1)
fmt.Println(result2)
Output:

[a b c d $1]
[a b c d 1]

func SplitEx added in v2.0.6

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 added in v2.1.16

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("a -b-c' 'd'e")
result4 := SplitWords("你好,我是一名码农")
result5 := SplitWords("こんにちは,私はプログラマーです")

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
Output:

[a word]
[I'am a programmer]
[a b-c' d'e]
[]
[]

func StringToBytes added in v2.1.20

func StringToBytes(str string) (b []byte)

StringToBytes converts a string to byte slice without a memory allocation. Play: https://go.dev/play/p/7OyFBrf9AxA

Example
result1 := StringToBytes("abc")
result2 := reflect.DeepEqual(result1, []byte{'a', 'b', 'c'})

fmt.Println(result1)
fmt.Println(result2)
Output:

[97 98 99]
true

func Substring added in v2.1.13

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 Trim added in v2.2.0

func Trim(str string, characterMask ...string) string

Trim strips whitespace (or other characters) from the beginning and end of a string. The optional parameter `characterMask` specifies the additional stripped characters. Play: https://go.dev/play/p/Y0ilP0NRV3j

Example
result1 := Trim("\nabcd")

str := "$ ab	cd $ "

result2 := Trim(str)
result3 := Trim(str, "$")

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
Output:

abcd
$ ab	cd $
ab	cd

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 added in v2.1.12

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 added in v2.1.12

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 added in v2.1.16

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("a -b-c' 'd'e")
result4 := WordCount("你好,我是一名码农")
result5 := WordCount("こんにちは,私はプログラマーです")

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
Output:

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