stringutil

package
v1.0.14 Latest Latest
Warning

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

Go to latest
Published: May 22, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

* Package stringutil to provides utility api for string operation

Index

Examples

Constants

View Source
const (
	INDEX_NOT_FOUND = -1
	EMPTY_STRING    = ""
)

Variables

This section is empty.

Functions

func Abbreviate

func Abbreviate(str, abbrevMarker string, offset, maxWidth int) (string, error)

Abbreviates a String using a given replacement marker

Example
s, err := stringutil.Abbreviate("abcdefghijklmno", "---", -1, 10)
fmt.Println(s, err)

s, err = stringutil.Abbreviate("abcdefghijklmno", ",", 0, 10)
fmt.Println(s, err)

s, err = stringutil.Abbreviate("abcdefghijklmno", "...", 6, 10)
fmt.Println(s, err)
Output:

abcdefg--- <nil>
abcdefghi, <nil>
...ghij... <nil>

func AbbreviateMiddle

func AbbreviateMiddle(str, middle string, length int) string

AbbreviateMiddle a String to the length passed, replacing the middle characters with the supplied replacement String.

Example
s := stringutil.AbbreviateMiddle("abcdef", ".", 4)
fmt.Println(s)

s = stringutil.AbbreviateMiddle("abc", ".", 3)
fmt.Println(s)
Output:

ab.f
abc

func Capitalize

func Capitalize(s string) string

Capitalize a String changing the first letter to upper case str.Capitalize("hello") = "Hello" str.Capitalize("HEllo") = "HEllo" str.Capitalize("") = "" str.Capitalize("12h") = "12h"

Example
ret := stringutil.Capitalize("hello")
fmt.Println(ret)

ret = stringutil.Capitalize("121H")
fmt.Println(ret)

ret = stringutil.Capitalize("HEllo")
fmt.Println(ret)
Output:

Hello
121H
HEllo

func Expand added in v1.0.6

func Expand(s, prefix, suffix string, fn base.Func[string, string]) (string, error)

Expand to solve place holder value with prefix and suffix and replace by 'fn' call back function

Example
// Expand simple expression
v, err := stringutil.Expand("please send mail to ${name} at ${address}!", "${", "}", func(placeholderKey string) string {
	mp := map[string]string{"name": "matt", "address": "shanghai pudong"}
	return mp[placeholderKey]
})
if err == nil {
	fmt.Println(v)
}

// Expand recursive expression
v, err = stringutil.Expand("please send mail to #{to#{name}} at #{address}", "#{", "}", func(placeholderKey string) string {
	mp := map[string]string{"name": "matt", "tomatt": "matt's company", "address": "shanghai pudong"}
	return mp[placeholderKey]
})
if err == nil {
	fmt.Println(v)
}
Output:

please send mail to matt at shanghai pudong!
please send mail to matt's company at shanghai pudong

func IndexFromOffset added in v1.0.6

func IndexFromOffset(s, sub string, fromIndex int) int

IndexFromOffset to return index of sub from Index

func IsBlank

func IsBlank(s string) bool

IsBlank return if s is empty or blank string IsBlank("") == true IsBlank(" ") == true IsBlank(" a ") == false

Example
fmt.Println(stringutil.IsBlank(""))
fmt.Println(stringutil.IsBlank(" "))
fmt.Println(stringutil.IsBlank(" a"))
Output:

true
true
false

func IsEmpty

func IsEmpty(s string) bool

IsEmpty return if s is empty

Example
fmt.Println(stringutil.IsEmpty(""))
fmt.Println(stringutil.IsEmpty(" "))
Output:

true
false

func Repeat

func Repeat(s byte, count int) string

fulfill string by repeat target count of byte

Example
s := stringutil.Repeat(97, 10)
fmt.Println(s)
Output:

aaaaaaaaaa

func ReplaceByOffset added in v1.0.6

func ReplaceByOffset(s string, begin, end int, replace string) string

ReplaceByOffset to replace sub string by offset begin and end index

func Reverse

func Reverse(s string) (string, error)

Reverse to reverse the string

Example
ret, err := stringutil.Reverse("hello")
fmt.Println(ret, err)

ret, err = stringutil.Reverse("hellochinese")
fmt.Println(ret, err)
Output:

olleh <nil>
esenihcolleh <nil>

func SliceToString

func SliceToString(b []byte) string

SliceToString base on unsafe packge to convert []byte to string without copy action key point: copy slice's Data and Len to string's Data and Len.

func StringToSlice

func StringToSlice(value string) []byte

StringToSlice base on unsafe package to convert string to []byte without copy action key point: copy string's Data and Len to slice's Data and Len, and append Cap value

Example
s := "hello world"
arr := stringutil.StringToSlice(s)
fmt.Println(string(arr))
Output:

hello world

func SubString

func SubString(s string, beginIndex, endIndex int) string

SubString a string that is a substring of this string.

func SubstringAfter

func SubstringAfter(s string, separator string) string

SubstringAfter Gets the substring after the first occurrence of a separator

Example
s := "abc"
r := stringutil.SubstringAfter(s, "a")
fmt.Println(r)

s = "abcba"
r = stringutil.SubstringAfter(s, "b")
fmt.Println(r)
Output:

bc
cba

func SubstringAfterLast

func SubstringAfterLast(s string, separator string) string

SubstringAfterLast Gets the substring after the last occurrence of a separator.

Example
s := "abc"
r := stringutil.SubstringAfterLast(s, "a")
fmt.Println(r)

s = "abcba"
r = stringutil.SubstringAfterLast(s, "b")
fmt.Println(r)
Output:

bc
a

func SubstringBefore

func SubstringBefore(s string, separator string) string

SubstringBefore Gets the substring before the first occurrence of a separator

Example
s := "helloworld to beijin"
separator := "wor"

// exist
ns := stringutil.SubstringBefore(s, separator)
fmt.Println(ns)

s = "helloworld hello world"
ns = stringutil.SubstringBefore(s, separator)
fmt.Println(ns)
Output:

hello
hello

func SubstringBeforeLast

func SubstringBeforeLast(s string, separator string) string

SubstringBeforeLast Gets the substring before the last occurrence of a separator

Example
s := "helloworldtobeijin"
separator := "wor"

// exist
ns := stringutil.SubstringBeforeLast(s, separator)
fmt.Println(ns)

ns = stringutil.SubstringBeforeLast(s, "notexist")
fmt.Println(ns)

s = "helloworldhelloworld"
ns = stringutil.SubstringBeforeLast(s, separator)
fmt.Println(ns)
Output:

hello
helloworldtobeijin
helloworldhello

func SubstringMatch added in v1.0.6

func SubstringMatch(s string, index int, sub string) bool

SubstringMatch to returns whether the given string matches the given substring

func Uncapitalize

func Uncapitalize(s string) string

Uncapitalize a String changing the first letter to lower case str.Capitalize("hello") = "hello" str.Capitalize("HEllo") = "hEllo" str.Capitalize("") = "" str.Capitalize("12h") = "12h"

Example
ret := stringutil.Uncapitalize("HEllo")
fmt.Println(ret)

ret = stringutil.Uncapitalize("hello")
fmt.Println(ret)
Output:

hEllo
hello

func Wrap

func Wrap(s string, wrap string) string

Wrap Wraps a String with another String.

Example
s := stringutil.Wrap("hello", "|")
fmt.Println(s)
Output:

|hello|

Types

This section is empty.

Jump to

Keyboard shortcuts

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