Documentation ¶
Overview ¶
* Package stringutil to provides utility api for string operation
Index ¶
- Constants
- func Abbreviate(str, abbrevMarker string, offset, maxWidth int) (string, error)
- func AbbreviateMiddle(str, middle string, length int) string
- func Capitalize(s string) string
- func Expand(s, prefix, suffix string, fn base.Func[string, string]) (string, error)
- func IndexFromOffset(s, sub string, fromIndex int) int
- func IsBlank(s string) bool
- func IsEmpty(s string) bool
- func Repeat(s byte, count int) string
- func ReplaceByOffset(s string, begin, end int, replace string) string
- func Reverse(s string) (string, error)
- func SliceToString(b []byte) string
- func StringToSlice(value string) []byte
- func SubString(s string, beginIndex, endIndex int) string
- func SubstringAfter(s string, separator string) string
- func SubstringAfterLast(s string, separator string) string
- func SubstringBefore(s string, separator string) string
- func SubstringBeforeLast(s string, separator string) string
- func SubstringMatch(s string, index int, sub string) bool
- func Uncapitalize(s string) string
- func Wrap(s string, wrap string) string
Examples ¶
Constants ¶
const ( INDEX_NOT_FOUND = -1 EMPTY_STRING = "" )
Variables ¶
This section is empty.
Functions ¶
func Abbreviate ¶
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 ¶
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 ¶
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
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
IndexFromOffset to return index of sub from Index
func IsBlank ¶
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 ¶
IsEmpty return if s is empty
Example ¶
fmt.Println(stringutil.IsEmpty("")) fmt.Println(stringutil.IsEmpty(" "))
Output: true false
func Repeat ¶
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
ReplaceByOffset to replace sub string by offset begin and end index
func Reverse ¶
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 ¶
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 ¶
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 SubstringAfter ¶
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 ¶
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 ¶
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 ¶
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
SubstringMatch to returns whether the given string matches the given substring
func Uncapitalize ¶
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
Types ¶
This section is empty.