strKit

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2023 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package strKit

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

Package strKit

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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendIfMissing

func AppendIfMissing(str, suffix string) string

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

PS: 区分大小写.

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

func AssertStringNotBlank added in v2.0.17

func AssertStringNotBlank(str string) error

func AssertStringNotEmpty added in v2.0.17

func AssertStringNotEmpty(str 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 Contains

func Contains(s, substr string) bool

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

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

func ContainsIgnoreCase

func ContainsIgnoreCase(s, substr string) bool

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

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

func Count

func Count(s, substr string) int

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

func EmptyToDefault

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

EmptyToDefault

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

func EndWith

func EndWith(s, suffix string) bool

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

func Equals

func Equals(str, str1 string) bool

Equals 比较字符串(区分大小写)

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

func EqualsIgnoreCase

func EqualsIgnoreCase(str, str1 string) bool

EqualsIgnoreCase 比较字符串(不区分大小写)

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

func Format deprecated

func Format(format string, args ...interface{}) string

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

Deprecated: 直接用 fmt.Sprintf() 对编码更友好。

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 Index

func Index(s, str string) int

Index

PS: (1) s中不存在substr的话,返回-1 (2) s中存在多个substr的话,返回第一个的下标

@param s 被查找的字符串 @param str 查找的字符串

e.g. ("abcabc", "ab") => 0 ("bcabc", "ab") => 2 ("23", "1") => -1

func IntToString

func IntToString(i int) string

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 Join

func Join(elements []string, sep string) string

func LastIndex

func LastIndex(s, str string) int

LastIndex

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

func PrependIfMissing

func PrependIfMissing(str, prefix string) string

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

PS: 区分大小写.

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

func RemovePrefixIfExist

func RemovePrefixIfExist(s, prefix string) string

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

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

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

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

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

func RemoveSpace

func RemoveSpace(str string) string

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

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

func RemoveSuffixIfExist

func RemoveSuffixIfExist(s, suffix string) string

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

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

func Replace

func Replace(s, old, new string, n int) string

func ReplaceAll

func ReplaceAll(s, old, new string) string

ReplaceAll

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

func ReplaceSpacesWithSpace

func ReplaceSpacesWithSpace(str string) string

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

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

func Split

func Split(s, sep string) []string

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

func StartWith

func StartWith(s, prefix string) bool

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

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(str string, from, to int) string

Substring 类似:Java的String.substring()

@param from 开始的下标(包括) @param to 结束的下标(不包括) @return 范围: [from, to)

要点: (1) from和to都不能 < 0 (2) 必须满足条件: from <= to(如果from == to,将返回"") (3) 下标不能越界

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

func ToBytes

func ToBytes(str string) []byte

ToBytes string => []byte

func ToLower

func ToLower(s string) string

func ToString

func ToString(obj interface{}) string

ToString

e.g. (nil) => ""

func ToStringE

func ToStringE(obj interface{}) (string, error)

ToStringE

e.g. (nil) => "", nil

func ToUpper

func ToUpper(s string) string

func Trim

func Trim(str string) string

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

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

Types

This section is empty.

Jump to

Keyboard shortcuts

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