strKit

package
v3.0.924 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package strKit

空白包括: " "(空格)、\r、\n、\t、\f

Package strKit

PS: 字符串间的比较可以用 "==" 或 "strings.Compare()";

Index

Constants

This section is empty.

Variables

View Source
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
)
View Source
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
)
View Source
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
View Source
var Contains func(s, substr string) bool = strings.Contains

Contains 是否包含(区分大小写)

e.g. ("", "1") => false ("abc", "Abc") => false

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

View Source
var CutPrefix func(s, prefix string) (after string, found bool) = strings.CutPrefix

CutPrefix

e.g. fmt.Println(strKit.CutPrefix("abcd", "ab")) // cd true fmt.Println(strKit.CutPrefix("abcd", "e")) // abcd false

View Source
var CutSuffix func(s, suffix string) (before string, found bool) = strings.CutSuffix
View Source
var (
	// Elipse 将字符串截断为指定长度,并在截断后附加省略号.
	/*
		e.g.
		("Richelieu", 0) => "..."
		("Richelieu", 1) => "..."
		("Richelieu", 2) => "..."
		("Richelieu", 3) => "..."
		("Richelieu", 4) => "R..."
		("Richelieu", math.MaxInt) => "Richelieu"
	*/
	Elipse func(str string, length int) string = lo.Elipse
)
View Source
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

View Source
var EqualsIgnoreCase func(str, str1 string) bool = strings.EqualFold

EqualsIgnoreCase 字符串是否相等?(不区分大小写)

e.g. ("abc", "Abc") => true

View Source
var Format func(format string, args ...interface{}) string = fmt.Sprintf

Format 格式化文本,类似Java的"StrUtil.format".

Deprecated: 直接用 fmt.Sprintf() 对编码更友好,比如GoLand.

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

View Source
var Join func(elements []string, sep string) string = strings.Join
View Source
var LastIndex func(s, str string) int = strings.LastIndex

LastIndex

e.g. ("", "1") => -1

View Source
var RemovePrefixIfExists func(s, prefix string) string = strings.TrimPrefix

RemovePrefixIfExists 去掉指定的"前缀"(如果存在的话)

PS: (1) 区分大小写; (2) 存在多个的话,只会移除第1个.

@param s 如果为"",返回"" @param prefix 如果为"",返回传参s

e.g. fmt.Println(strKit.RemovePrefixIfExists("abcd", "ab")) // cd fmt.Println(strKit.RemovePrefixIfExists("abcd", "e")) // abcd

e.g.1 ""的情况 ("", "") => "" ("1", "") => "1" ("", "1") => ""

e.g.1 区分大小写 ("abcd", "abcd") => "" ("abcd", "Abcd") => "abcd"

View Source
var RemoveSuffixIfExists func(s, suffix string) string = strings.TrimSuffix

RemoveSuffixIfExists 去掉指定的"后缀"(如果存在的话)

PS: (1) 区分大小写; (2) 存在多个的话,只会移除最后1个.

View Source
var Replace func(s, old, new string, n int) string = strings.Replace
View Source
var ReplaceAll func(s, old, new string) string = strings.ReplaceAll

ReplaceAll

e.g. ("12321", "2", "0") => "10301"

View Source
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,第三个元素为"")

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

View Source
var ToLower func(s string) string = strings.ToLower
View Source
var ToUpper func(s string) string = strings.ToUpper
View Source
var TrimSpace func(str string) string = strings.TrimSpace

TrimSpace 去掉左右的空格(包括:\r、\n),不包括中间的空格.

PS: Golang中所有的传参都是值传递(传值),都是一个副本,一个拷贝.

Functions

func AppendIfMissing

func AppendIfMissing(str, suffix string) (rst string)

AppendIfMissing 如果给定字符串不是以给定的字符串为结尾,则在"尾部"添加结尾字符串(不忽略大小写).

PS: 区分大小写.

@param suffix 后缀

e.g. ("abc", "c")) => "abc" ("abc", "C")) => "abcC" ("abc", "0")) => "abc0"

func AssertNotBlank

func AssertNotBlank(str string, paramName string) error

func AssertNotEmpty

func AssertNotEmpty(str string, paramName string) error

func BlankToDefault

func BlankToDefault(str, def string) string

func BytesToString

func BytesToString(b []byte) string

BytesToString converts byte slice to string without a memory allocation.

PS: (1) copy from gin/internal/bytesconv/bytesconv.go (2) Richelieu: 感觉这样效率更高,但慎用!!!

func ContainsIgnoreCase

func ContainsIgnoreCase(s, substr string) bool

ContainsIgnoreCase 是否包含(不区分大小写)

e.g. ("abc", "Abc") => true ("abcde", "BC") => true

func EmptyToDefault

func EmptyToDefault(str, def string, trimArgs ...bool) string

EmptyToDefault

@param trimArgs 是否先对 传参str 进行trim处理?(默认:false,不处理)

func Equals

func Equals(str, str1 string) bool

Equals 字符串是否相等?(区分大小写)

PS: 也可以用 == 进行判断字符串是否相等(但不建议这么干).

e.g. ("abc", "Abc") => false

func GetChineseRuneCount

func GetChineseRuneCount(str string) (count int)

GetChineseRuneCount 获取字符串中 中文字符 的个数

PS: 包括简体、繁体.

func GetRuneCount

func GetRuneCount(str string) int

GetRuneCount

PS: (1) 包括简体、繁体. (2) 如果确定 传参str 中不存在中文字符,建议直接使用 内置函数len().

@param str (可能)带有 中文字符 的字符串

e.g. ("test") => 4 ("测试") => 2

func HasChineseRune

func HasChineseRune(str string) bool

HasChineseRune 字符串中是否存在 中文字符?

PS: 包括简体、繁体.

func HasEmpty

func HasEmpty(strings ...string) bool

func IsAllEmpty

func IsAllEmpty(strings ...string) bool

func IsAllNotEmpty

func IsAllNotEmpty(strings ...string) bool

func IsBlank

func IsBlank(str string) bool

IsBlank

(" \r\n ") => true

func IsEmpty

func IsEmpty(str string) bool

IsEmpty strings.Compare比自建方法“==”的速度要快

PS: 由于此方法被调用的频率较高,因此方法体内禁止调用自己写的方法,全用原生的.

func IsEmptyString

func IsEmptyString(obj interface{}) bool

func IsNotEmpty

func IsNotEmpty(str string) bool

func PrependIfMissing

func PrependIfMissing(str, prefix string) (rst string)

PrependIfMissing 如果给定字符串不是以给定的字符串为开头,则在"首部"添加 起始字符串.

PS: 区分大小写.

@param prefix 前缀

e.g. ("abc", "a") => "abc" ("abc", "A")) => "Aabc" ("abc", "0") => "0abc"

func RemoveSpace

func RemoveSpace(str string) string

RemoveSpace 移除str中所有的(包括左右的、中间的):" "、"\t"、"\r"、"\n"...

e.g. (" \t\r\n \n\n") => ""

func ReplaceSpacesWithSpace

func ReplaceSpacesWithSpace(str string) string

ReplaceSpacesWithSpace 将连续的空格替换为单个空格

e.g. "It\tis\na\fsimple\rtest !" => "It is a simple test !"

func String added in v3.0.910

func String(v string) *string

func StringToBytes

func StringToBytes(s string) []byte

StringToBytes converts string to byte slice without a memory allocation.

PS: (1) copy from gin/internal/bytesconv/bytesconv.go (2) Richelieu: 感觉这样效率更高,但慎用!!!

func SubAfter

func SubAfter(s string, index int) string

SubAfter

@param index 包括 @return 范围: [index, length)

func SubAfterString

func SubAfterString(s, str string) string

SubAfterString

case 1: s包含str的情况,返回截取后的字符串; case 2: s不包含str的情况,直接返回s.

e.g. ("abcd", "bc") => "bcd"

func SubBefore

func SubBefore(s string, index int) string

SubBefore

@param index 不包括 @return 范围: [0, index)

func SubBeforeString

func SubBeforeString(s, str string) string

SubBeforeString

case 1: s包含str的情况,返回截取后的字符串; case 2: s不包含str的情况,直接返回s.

e.g. ("abcd", "bc") => "a"

func Substring

func Substring[T ~string](str T, offset int, length uint) T

Substring Return part of a string.

@param offset 开始下标(甚至可以<0, -1: 最后一个字符)

e.g.

("hello", -1, 1)			=> "o"
("hello", 2, 3)				=> "llo"
("hello", -4, 3)			=> "ell"
("hello", -2, math.MaxUint)	=> "lo"

Types

This section is empty.

Jump to

Keyboard shortcuts

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