Documentation ¶
Overview ¶
Package strKit
空白包括: " "(空格)、\r、\n、\t、\f
Package strKit ¶
PS: 字符串间的比较可以用 "==" 或 "strings.Compare()";
Index ¶
- Variables
- func AppendIfMissing(str, suffix string) (rst string)
- func AssertNotBlank(str string, name string) error
- func AssertNotEmpty(str string, name string) error
- func BlankToDefault(str, def string) string
- func BytesToString(b []byte) string
- func ContainsIgnoreCase(s, substr string) bool
- func EmptyToDefault(str, def string, trimArgs ...bool) string
- func Equals(str, str1 string) bool
- func GetChineseRuneCount(str string) (count int)
- func GetRuneCount(str string) int
- func HasChineseRune(str string) bool
- func HasEmpty(strings ...string) bool
- func IsAllEmpty(strings ...string) bool
- func IsAllNotEmpty(strings ...string) bool
- func IsBlank(str string) bool
- func IsEmpty(str string) bool
- func IsEmptyString(obj interface{}) bool
- func IsNotEmpty(str string) bool
- func PrependIfMissing(str, prefix string) (rst string)
- func RemoveSpace(str string) string
- func ReplaceSpacesWithSpace(str string) string
- func StringToBytes(s string) []byte
- func SubAfter(s string, index int) string
- func SubAfterString(s, str string) string
- func SubBefore(s string, index int) string
- func SubBeforeString(s, str string) string
- func Substring(str string, from, to int) string
Constants ¶
This section is empty.
Variables ¶
var ( ToString func(i interface{}) string = cast.ToString ToStringE func(i interface{}) (string, error) = cast.ToStringE // IntToString int(10进制) => string /* PS: strconv.Itoa(i) <=> FormatInt(int64(i), 10) */ IntToString func(i int) string = strconv.Itoa // IntToStringWithBase int(各种进制) => string /* @param base 传参i的进制数 */ IntToStringWithBase func(i int64, base int) string = strconv.FormatInt )
var ( // Trim 返回字符串,删除了s"左边&&右边"的连续cutset. /* e.g. fmt.Println(strings.Trim("aaa0aaa0aaa", "a")) // "0aaa0" fmt.Println(strings.Trim(" aba ", "a")) // " aba " fmt.Println(strings.Trim("/a/", "/")) // "a" */ Trim func(s, cutset string) string = strings.Trim // TrimLeft 返回字符串,删除了s"左边"的连续cutset. /* e.g. fmt.Println(strings.TrimLeft("aaa0aaa0aaa", "a")) // "0aaa0aaa" fmt.Println(strings.TrimLeft(" aba ", "a")) // " aba " */ TrimLeft func(s, cutset string) string = strings.TrimLeft // TrimRight 返回字符串,删除了s"右边"的连续cutset. /* e.g. fmt.Println(strings.TrimRight("aaa0aaa0aaa", "a")) // "aaa0aaa0" fmt.Println(strings.TrimRight(" aba ", "a")) // " aba " */ TrimRight func(s, cutset string) string = strings.TrimRight // TrimFunc 返回字符串,删除了s"左边&&右边"的连续的满足条件的rune. /* @param f (1) 返回true: 删除此rune,继续判断下一个rune (2) 返回false: 不删除此rune,中断此次(左边||右边)删除 e.g. str := strings.TrimFunc("77GeeksForGeeks!!!11a1", func(r rune) bool { return unicode.IsDigit(r) }) fmt.Print(str) // "GeeksForGeeks!!!11a" */ TrimFunc func(s string, f func(rune) bool) string = strings.TrimFunc // TrimLeftFunc 返回字符串,删除了s"左边"的连续的满足条件的rune. /* @param f (1) 返回true: 删除此rune,继续判断下一个rune (2) 返回false: 不删除此rune,中断此次(左边||右边)删除 */ TrimLeftFunc func(s string, f func(rune) bool) string = strings.TrimLeftFunc // TrimRightFunc 返回字符串,删除了s"右边"的连续的满足条件的rune. /* @param f (1) 返回true: 删除此rune,继续判断下一个rune (2) 返回false: 不删除此rune,中断此次(左边||右边)删除 */ TrimRightFunc func(s string, f func(rune) bool) string = strings.TrimRightFunc )
var Compare func(a, b string) int = strings.Compare
Compare 字符串比较
@return (1) -1: if a < b
(2) 0: if a == b (3) +1: if a > b
var Contains func(s, substr string) bool = strings.Contains
Contains 是否包含(区分大小写)
e.g. ("", "1") => false ("abc", "Abc") => false
var Count func(s, substr string) int = strings.Count
Count 计数
@return >= 0
e.g. ("12345", "3") => 1 ("12345", "6") => 0
e.g.1 If substr is an empty string, Count returns 1 + the number of Unicode code points in s. strKit.Count("12345", "") => 6
var EndWith func(s, suffix string) bool = strings.HasSuffix
EndWith
PS: 区分大小写.
@param s suffix不为""的情况下,如果s为"",返回值必定为false @param suffix 如果suffix为"",返回值必定为true
e.g. ""的情况 ("", "") => true ("1", "") => true ("", "1") => false
e.g.1 区分大小写 ("abc", "abc") => true ("abc", "Abc") => false
var EqualsIgnoreCase func(str, str1 string) bool = strings.EqualFold
EqualsIgnoreCase 字符串是否相等?(不区分大小写)
e.g. ("abc", "Abc") => true
var Format func(format string, args ...interface{}) string = fmt.Sprintf
Format 格式化文本,类似Java的"StrUtil.format".
Deprecated: 直接用 fmt.Sprintf() 对编码更友好,比如GoLand.
var Index func(s, str string) int = strings.Index
Index
PS: (1) s中不存在substr的话,返回 -1 (2) s中存在多个substr的话,返回 第一个的下标
@param s 被查找的字符串 @param str 查找的字符串
e.g. ("abcabc", "ab") => 0 ("bcabc", "ab") => 2 ("23", "1") => -1
var LastIndex func(s, str string) int = strings.LastIndex
LastIndex
e.g. ("", "1") => -1
var RemovePrefixIfExists func(s, prefix string) string = strings.TrimPrefix
RemovePrefixIfExists 去掉指定的"前缀"(如果存在的话)
PS: (1) 区分大小写; (2) 存在多个的话,只会移除第1个.
@param s 如果为"",返回"" @param prefix 如果为"",返回传参s
e.g. ""的情况 ("", "") => "" ("1", "") => "1" ("", "1") => ""
e.g.1 区分大小写 ("abcd", "abcd") => "" ("abcd", "Abcd") => "abcd"
var RemoveSuffixIfExists func(s, suffix string) string = strings.TrimSuffix
RemoveSuffixIfExists 去掉指定的"后缀"(如果存在的话)
PS: (1) 区分大小写; (2) 存在多个的话,只会移除最后1个.
var ReplaceAll func(s, old, new string) string = strings.ReplaceAll
ReplaceAll
e.g. ("12321", "2", "0") => "10301"
var Split func(s, sep string) []string = strings.Split
Split
@param sep 可以为"",此种情况比较特殊,详见下例;
如果s中不存在sep,那么返回切片的长度为1
@return 必定不为nil && len >= 1
e.g. ("", "-") => [](长度为1;唯一的元素为"") ("-", "-") => []string{"", ""} ("123-", "-") => []string{"123", ""} e.g.1 ("hello world!", "") => [h e l l o w o r l d !] ("1-2-3", "-") => [1 2 3](长度为3) ("1-2-3", "+") => [1-2-3](长度为1) ("172.18.21.50;8095;", ";") => [172.18.21.50 8095 ](长度为3,第三个元素为"")
var StartWith func(s, prefix string) bool = strings.HasPrefix
StartWith
PS: 区分大小写.
@param s prefix不为""的情况下,如果s为"",返回值必定为false @param prefix 如果prefix为"",返回值必定为true
e.g. ""的情况 ("", "") => true ("1", "") => true ("", "1") => false
e.g.1 区分大小写 ("abc", "abc") => true ("abc", "Abc") => false
var ToLower func(s string) string = strings.ToLower
var ToUpper func(s string) string = strings.ToUpper
var TrimSpace func(str string) string = strings.TrimSpace
TrimSpace 去掉左右的空格(包括:\r、\n),不包括中间的空格.
PS: Golang中所有的传参都是值传递(传值),都是一个副本,一个拷贝.
Functions ¶
func AppendIfMissing ¶
AppendIfMissing 如果给定字符串不是以给定的字符串为结尾,则在"尾部"添加结尾字符串(不忽略大小写).
PS: 区分大小写.
@param suffix 后缀
e.g. ("abc", "c")) => "abc" ("abc", "C")) => "abcC" ("abc", "0")) => "abc0"
func AssertNotBlank ¶ added in v2.1.7
func AssertNotEmpty ¶ added in v2.1.7
func BlankToDefault ¶
func BytesToString ¶
BytesToString converts byte slice to string without a memory allocation.
PS: (1) copy from gin/internal/bytesconv/bytesconv.go (2) Richelieu: 感觉这样效率更高,但慎用!!!
func ContainsIgnoreCase ¶
ContainsIgnoreCase 是否包含(不区分大小写)
e.g. ("abc", "Abc") => true ("abcde", "BC") => true
func GetRuneCount ¶
GetRuneCount
PS: (1) 包括简体、繁体. (2) 如果确定 传参str 中不存在中文字符,建议直接使用 内置函数len().
@param str (可能)带有 中文字符 的字符串
e.g. ("test") => 4 ("测试") => 2
func IsAllEmpty ¶
func IsAllNotEmpty ¶
func IsEmptyString ¶
func IsEmptyString(obj interface{}) bool
func IsNotEmpty ¶
func PrependIfMissing ¶
PrependIfMissing 如果给定字符串不是以给定的字符串为开头,则在"首部"添加 起始字符串.
PS: 区分大小写.
@param prefix 前缀
e.g. ("abc", "a") => "abc" ("abc", "A")) => "Aabc" ("abc", "0") => "0abc"
func RemoveSpace ¶
RemoveSpace 移除str中所有的(包括左右的、中间的):" "、"\t"、"\r"、"\n"...
e.g. (" \t\r\n \n\n") => ""
func ReplaceSpacesWithSpace ¶
ReplaceSpacesWithSpace 将连续的空格替换为单个空格
e.g. "It\tis\na\fsimple\rtest !" => "It is a simple test !"
func StringToBytes ¶
StringToBytes converts string to byte slice without a memory allocation.
PS: (1) copy from gin/internal/bytesconv/bytesconv.go (2) Richelieu: 感觉这样效率更高,但慎用!!!
func SubAfterString ¶
SubAfterString
case 1: s包含str的情况,返回截取后的字符串; case 2: s不包含str的情况,直接返回s.
e.g. ("abcd", "bc") => "bcd"
func SubBeforeString ¶
SubBeforeString
case 1: s包含str的情况,返回截取后的字符串; case 2: s不包含str的情况,直接返回s.
e.g. ("abcd", "bc") => "a"
Types ¶
This section is empty.