Versions Compared
Key
- This line was added.
- This line was removed.
- Formatting was changed.
gstr
提供了强大便捷的文本处理组件,组件内置了大量常用的字符串处理方法,比较于Golang
标准库更加全面丰富,可应对绝大部分业务场景。
使用方式:
Code Block | ||
---|---|---|
| ||
import "github.com/gogf/gf/v2/text/gstr" |
接口文档:
https://godocpkg.go.orgdev/github.com/gogf/gf/v2/text/gstr
Tip |
---|
以下常用方法列表,文档更新可能滞后于代码新特性,更多的方法及示例请参考代码文档:https://pkg.go.dev/github.com/gogf/gf/v2/text/gstr |
字符串判断
Count
IsNumeric
说明:
Count
计算substr
在s
中出现的次数。 如果在s
中没有找到substr
,则返回0
。IsNumeric
验证字符串s
是否为数字。格式:
Code Block language go CountIsNumeric(s, substr string) intbool
示例:
Code Block language go func ExampleCountExampleIsNumeric() { fmt.Println(gstr.IsNumeric("88")) fmt.Println(gstr.IsNumeric("3.1415926")) fmt.Println(gstr.IsNumeric("abc")) // Output: // true // true // false }
字符串长度
LenRune
说明:
LenRune
返回unicode
字符串长度。格式:
Code Block language go LenRune(str string) int
示例:
Code Block language go func ExampleLenRune() { var ( str = `GoFrame框架` result = gstr.LenRune(str var ( str = `goframe is very, very easy to use` substr1 = "goframe" substr2 = "very" result1 = gstr.Count(str, substr1) result2 = gstr.Count(str, substr2) ) fmt.Println(result1result) fmt.Println(result2) // Output: // 1 // 29 }
CountI
字符串创建
Repeat
说明:
Repeat
返回一个由input
重复multiplier
次后组成的新字符串。 说明:Count
计算substr
在s
中出现的次数,不区分大小写。 如果在s
中没有找到substr
,则返回0
。格式:
Code Block language go CountIRepeat(sinput string, substrmultiplier stringint) intstring
示例:
Code Block language go func ExampleCountIExampleRepeat() { var ( strinput = `goframe is very, very easy to use`` substr1multiplier = "GOFRAME"3 substr2result = "VERY" result1 = gstr.CountIRepeat(strinput, substr1multiplier) result2 = gstr.CountI(str, substr2) ) fmt.Println(result1) fmt.Println(result2) ) fmt.Println(result) // Output: // 1 // 2goframe goframe goframe }
大小写转换
ToLower
说明:
ToLower
将s
中所有Unicode
字符都变为小写并返回其副本。格式:
Code Block language go ToLower(s string) string
示例:
Code Block language go func ExampleToLower() { var ( s = `GOFRAME` result = gstr.ToLower(s) ) fmt.Println(result) // Output: // goframe }
ToUpper
说明:
ToUpper
将s
中所有Unicode
字符都变为大写并返回其副本。格式:
Code Block language go ToUpper(s string) string
示例:
Code Block language go func ExampleToUpper() { var ( s = `goframe` result = gstr.ToUpper(s) ) fmt.Println(result) // Output: // GOFRAME }
UcFirst
说明:
UcFirst
将s
中首字符变为大写并返回其副本。格式:
Code Block language go UcFirst(s string) string
示例:
Code Block language go func ExampleUcFirst() { var ( s = `hello` result = gstr.UcFirst(s) ) fmt.Println(result) // Output: // Hello }
LcFirst
说明:
LcFirst
将s
中首字符变为小写并返回其副本。格式:
Code Block language go LcFirst(s string) string
示例:
Code Block language go func ExampleLcFirst() { var ( str = `Goframe` result = gstr.LcFirst(str) ) fmt.Println(result) // Output: // goframe }
UcWords
说明:
UcWords
将字符串str
中每个单词的第一个字符变为大写。格式:
Code Block language go UcWords(str string) string
示例:
Code Block language go func ExampleUcWords() { var ( str = `hello world` result = gstr.UcWords(str) ) fmt.Println(result) // Output: // Hello World }
IsLetterLower
说明:
IsLetterLower
验证给定的字符b
是否是小写字符。格式:
Code Block language go IsLetterLower(b byte) bool
示例:
Code Block language go func ExampleIsLetterLower() { fmt.Println(gstr.IsLetterLower('a')) fmt.Println(gstr.IsLetterLower('A')) // Output: // true // false }
IsLetterUpper
说明:
IsLetterUpper
验证字符b
是否是大写字符。格式:
Code Block language go IsLetterUpper(b byte) bool
示例:
Code Block language go func ExampleIsLetterUpper() { fmt.Println(gstr.IsLetterUpper('A')) fmt.Println(gstr.IsLetterUpper('a')) // Output: // true // false }
字符串比较
IsNumeric
Compare
说明:
Compare
返回一个按字典顺序比较两个字符串的整数。 如果a == b
,结果为0
,如果a < b
,结果为-1
,如果a > b
,结果为+1
。 说明:IsNumeric
验证字符串s
是否为数字。格式:
Code Block language go IsNumeric(sCompare(a, b string) boolint
示例:
Code Block language go func ExampleIsNumericExampleCompare() { fmt.Println(gstr.IsNumericCompare("88c", "c")) fmt.Println(gstr.IsNumericCompare("3.1415926a", "b")) fmt.Println(gstr.IsNumericCompare("abcc", "b")) // Output: // true0 // true-1 // false1 }
Reverse
Equal
说明:
Reverse
返回str
的反转字符串。Equal
返回a
和b
在不区分大小写的情况下是否相等。格式:
Code Block language go Reverse(strEqual(a, b string) stringbool
示例:
Code Block language go func ExampleReverseExampleEqual() { var ( str = `123456` result = gstr.Reverse(str) fmt.Println(gstr.Equal(`A`, `a`)) fmt.Println(gstr.Equal(`A`, `A`)) fmt.Println(resultgstr.Equal(`A`, `B`)) // Output: // 654321true // true // false }
NumberFormat
切分组合
Split
说明:
Split
用delimiter
将str
拆分为[]string
。说明:
NumberFormat
以千位分组来格式化数字。- 参数
decimal
设置小数点的个数。 - 参数
decPoint
设置小数点的分隔符。 参数
thousand
设置千位分隔符。
- 参数
格式:
Code Block language go NumberFormatSplit(number float64str, decimals int, decPoint, thousandsSep delimiter string) []string
示例:
Code Block language go func ExampleNumberFormatExampleSplit() { var ( number str float64 = 123456`a|b|c|d` decimals = 2delimiter = `|` decPointresult = "." thousandsSep = "," result = gstr.NumberFormat(number, decimals, decPoint, thousandsSep) ) fmt.Println(gstr.Split(str, delimiter) ) fmt.Printf(`%#v`, result) // Output: // 123,456.00 []string{"a", "b", "c", "d"} }
ChunkSplit
SplitAndTrim
说明:
SplitAndTrim
使用delimiter
将str
拆分为[]string
,并对[]string
的每个元素调用Trim
,并忽略在Trim
之后为空的元素。格式:
说明:
ChunkSplit
将字符串拆分为单位为chunkLen
长度更小的每一份,并用end
连接每一份拆分出的字符串。格式:
Code Block language go ChunkSplitSplitAndTrim(body stringstr, chunkLendelimiter intstring, endcharacterMask ...string) []string
示例:
Code Block language go func ExampleChunkSplitExampleSplitAndTrim() { var ( bodystr = `1234567890` chunkLen = 2`a|b|||||c|d` enddelimiter = "#"`|` result = gstr.ChunkSplitSplitAndTrim(bodystr, chunkLen, enddelimiter) ) fmt.PrintlnPrintf(`%#v`, result) // Output: // 12#34#56#78#90# }
Compare
[]string{"a", "b", "c", "d"} }
Join
说明:
Join
将array
中的每一个元素连接并生成一个新的字符串。参数sep
会作为新字符串的分隔符。说明:Compare
返回一个按字典顺序比较两个字符串的整数。 如果a == b
,结果为0
,如果a < b
,结果为-1
,如果a > b
,结果为+1
。格式:
Code Block language go Compare(aJoin(array []string, bsep string) intstring
示例:
Code Block language go func ExampleCompareExampleJoin() { fmt.Println(gstr.Compare("cvar ( array = []string{"goframe", "c")) fmt.Println(gstr.Compare("ais", "very", "b")) fmt.Println(gstr.Compare("c", "b")easy", "to", "use"} sep = ` ` result = gstr.Join(array, sep) ) fmt.Println(result) // Output: // 0 // -1 // 1 goframe is very easy to use }
Equal
JoinAny
说明:
Equal
返回a
和b
在不区分大小写的情况下是否相等。JoinAny
将array
中的每一个元素连接并生成一个新的字符串。参数sep
会作为新字符串的分隔符。参数array
可以是任意的类型。格式:
Code Block language go Equal(aJoinAny(array interface{}, bsep string) boolstring
示例:
Code Block language go func ExampleEqualExampleJoinAny() { fmt.Println(gstr.Equal(`A`, `a`)) fmt.Println(gstr.Equal(`A`, `A`)var ( sep = `,` arr2 = []int{99, 73, 85, 66} result = gstr.JoinAny(arr2, sep) ) fmt.Println(gstr.Equal(`A`, `B`))result) // Output: // true // true // false }
Fields
99,73,85,66 }
Explode
说明:
Explode
使用分隔符delimiter
字符串str
拆分成[]string
说明:Fields
以[]string
的形式返回字符串中的每个单词。格式:
Code Block language go FieldsExplode(delimiter, str string) []string
示例:
Code Block language go func ExampleFieldsExampleExplode() { var ( str = `Hello World` delimiter = " " result = gstr.FieldsExplode(delimiter, str) ) fmt.Printf(`%#v`, result) // Output: // []string{"Hello", "World"} }
HasPrefix
Implode
说明:
HasPrefix
返回s
是否以prefix
开头。Implode
使用glue
连接pieces
字符串数组的每一个元素。格式:
Code Block language go HasPrefixImplode(sglue string, prefixpieces []string) boolstring
示例:
Code Block language go func ExampleHasPrefixExampleImplode() { var ( spieces = []string{"goframe", "is", "very", = `Hello World`"easy", "to", "use"} prefixglue = "Hello " result = gstr.HasPrefixImplode(sglue, prefixpieces) ) fmt.Println(result) // Output: // true goframe is very easy to use }
HasSuffix
ChunkSplit
说明:
HasSuffix
返回s
是否以suffix
结束。ChunkSplit
将字符串拆分为单位为chunkLen
长度更小的每一份,并用end
连接每一份拆分出的字符串。格式:
Code Block language go HasSuffix(sChunkSplit(body string, chunkLen int, suffixend string) boolstring
示例:
Code Block language go func ExampleHasSuffixExampleChunkSplit() { var ( sbody = `1234567890` chunkLen = 2 end `my best love is goframe` prefix = "goframe#" result = gstr.HasSuffix(sChunkSplit(body, chunkLen, prefixend) ) fmt.Println(result) // Output: // true12#34#56#78#90# }
CountWords
Fields
说明:
CountWords
Fields
以map[]string
]int的形式返回str
中使用的单词的统计信息。的形式返回字符串中的每个单词。格式:
Code Block language go CountWordsFields(str string) map[]string]int
示例:
Code Block language go func ExampleCountWordsExampleFields() { var ( str = `goframe is very, very easy to use!``Hello World` result = gstr.CountWordsFields(str) ) fmt.Printf(`%#v`, result) // Output: // map[]string]int{"easyHello":1, "goframe":1, "is":1, "to":1, "use!":1, "very":1, "very,":1World"} }
转义处理
CountChars
AddSlashes
说明:
AddSlashes
将字符串中的符号前添加转义字符'\'
说明:CountChars
以map[string]int
的形式返回str
中使用的字符的统计信息。noSpace
参数可以控制是否计算空格。格式:
Code Block language go CountCharsAddSlashes(str string, noSpace ...bool) map[string]int) string
示例:
Code Block language go func ExampleCountCharsExampleAddSlashes() { var ( str = `goframe``'aa'"bb"cc\r\n\d\t` result = gstr.CountCharsAddSlashes(str) ) fmt.Println(result) // May Output: // map[a:1 e:1 f:1 g:1 m:1 o:1 r:1] }
WordWrap
\'aa\'\"bb\"cc\\r\\n\\d\\t }
StripSlashes
说明:
StripSlashes
去掉字符串str
中的转义字符'\'
。说明:WordWrap
使用换行符将str
换行到给定字符数(不会切分单词)。格式:
Code Block language go WordWrapStripSlashes(str string, width int, br string) string
示例:
Code Block language go func ExampleWordWrapExampleStripSlashes() { { var ( str = `A very long woooooooooooooooooord. and something` width = 8 br = "\n" `C:\\windows\\GoFrame\\test` result = gstr.WordWrapStripSlashes(str, width, br) ) fmt.Println(result) } { var ( str = `The quick brown fox jumped over the lazy dog.` width = 20 br = "<br />\n" // Output: // C:\windows\GoFrame\test }
QuoteMeta
说明:
QuoteMeta
为str中'. \ + * ? [ ^ ] ( $ )
中的每个字符前添加一个转义字符'\'。
格式:
Code Block language go QuoteMeta(str string, chars ...string) string
示例:
Code Block language go func ExampleQuoteMeta() { { var ( str = `.\+?[^]()` result = gstr.WordWrapQuoteMeta(str, width, br) ) fmt.PrintfPrintln("%v", result) } { //var Output:( str = `https://goframe.org/pages/ A very // long // woooooooooooooooooord. // andviewpage.action?pageId=1114327` result = gstr.QuoteMeta(str) ) fmt.Println(result) } // somethingOutput: // The quick brown fox<br />\.\\\+\?\[\^\]\(\) // jumped over the lazy<br /> // dog. }
LenRune
https://goframe\.org/pages/viewpage\.action\?pageId=1114327 }
统计计数
Count
说明:
Count
计算substr
在s
中出现的次数。 如果在s
中没有找到substr
,则返回0
。 说明:LenRune
返回unicode
字符串长度。格式:
Code Block language go LenRune(strCount(s, substr string) int
示例:
Code Block language go func ExampleLenRuneExampleCount() { var ( str = `GoFrame框架``goframe is very, very easy to use` resultsubstr1 = gstr.LenRune(str) "goframe" substr2 = "very" result1 = gstr.Count(str, substr1) result2 = gstr.Count(str, substr2) ) fmt.Println(result1) fmt.Println(resultresult2) // Output: // 1 // 92 }
Repeat
CountI
说明:
Count
计算substr
在s
中出现的次数,不区分大小写。 如果在s
中没有找到substr
,则返回0
。 说明:Repeat
返回一个由input
重复multiplier
次后组成的新字符串。格式:
Code Block language go RepeatCountI(input strings, multipliersubstr intstring) stringint
示例:
Code Block language go func ExampleRepeatExampleCountI() { var ( input str = `goframe ` multiplier is very, very easy to use` substr1 = 3"GOFRAME" resultsubstr2 = "VERY" result1 = gstr.CountI(str, substr1) result2 = gstr.RepeatCountI(inputstr, multipliersubstr2) ) fmt.Println(result1) fmt.Println(resultresult2) // Output: // goframe goframe goframe1 // 2 }
Shuffle
CountWords
说明:
Shuffle
返回将str
随机打散后的字符串。CountWords
以map[string]int
的形式返回str
中使用的单词的统计信息。格式:
Code Block language go ShuffleCountWords(str string) map[string]int
示例:
Code Block language go func ExampleShuffleExampleCountWords() { var ( str = `123456` result = gstr.Shuffle`goframe is very, very easy to use!` result = gstr.CountWords(str) ) fmt.PrintlnPrintf(`%#v`, result) // May Output: // 563214 map[string]int{"easy":1, "goframe":1, "is":1, "to":1, "use!":1, "very":1, "very,":1} }
Split
CountChars
说明:
Split
用delimiter
将str
拆分为[]string
。CountChars
以map[string]int
的形式返回str
中使用的字符的统计信息。noSpace
参数可以控制是否计算空格。格式:
Code Block 格式:
Code Block language go SplitCountChars(str string, delimiter stringnoSpace ...bool) map[string]stringint
示例:
Code Block language go func ExampleSplitExampleCountChars() { var ( str = `a|b|c|d``goframe` delimiter = `|` result = gstr.SplitCountChars(str, delimiter) ) fmt.PrintfPrintln(`%#v`, result) // May Output: // map[]string{"a", "b", "c", "d"} }
SplitAndTrim
a:1 e:1 f:1 g:1 m:1 o:1 r:1] }
数组处理
SearchArray
说明:
SearchArray
在[]string 'a'
中区分大小写地搜索字符串's'
,返回其在'a'
中的索引。 如果在'a'
中没有找到's'
,则返回-1
。 说明:SplitAndTrim
使用delimiter
将str
拆分为[]string
,并对[]string
的每个元素调用Trim
,并忽略在Trim
之后为空的元素。格式:
Code Block language go SplitAndTrimSearchArray(str, delimiter a []string, characterMasks ...string) []stringint
示例:
Code Block language go func ExampleSplitAndTrimExampleSearchArray() { var ( strarray = []string{"goframe", "is", = `a|b|||||c|d`"very", "nice"} delimiterstr = `|``goframe` result = gstr.SplitAndTrimSearchArray(strarray, delimiterstr) ) fmt.PrintfPrintln(`%#v`, result) // Output: // []string{"a", "b", "c", "d"}0 }
Join
InArray
说明:
InArray校验
[]string 'a'
中是否有字符串' s '
。 说明:Join
将array
中的每一个元素连接并生成一个新的字符串。参数sep
会作为新字符串的分隔符。格式:
Code Block language go JoinInArray(arraya []string, seps string) stringbool
示例:
Code Block language go func ExampleJoinExampleInArray() { var ( arraya = []string{"goframe", "is", "very", "easy", "to", "use"} seps = ` `"goframe" result = gstr.JoinInArray(arraya, seps) ) fmt.Println(result) // Output: // goframe is very easy to usetrue }
JoinAny
PrefixArray
说明:
JoinAny
将array
中的每一个元素连接并生成一个新的字符串。参数sep
会作为新字符串的分隔符。参数array
可以是任意的类型。PrefixArray
位[]string array
的每一个字符串添加'prefix'
的前缀。格式:
Code Block language go JoinAnyPrefixArray(array interface{}[]string, sepprefix string) string
示例:
Code Block language go func ExampleJoinAnyExamplePrefixArray() { var ( sep = `,` arr2 strArray = []intstring{99"tom", 73"lily", 85, 66"john"} result = ) gstr.JoinAnyPrefixArray(arr2strArray, sep"classA_") ) fmt.Println(resultstrArray) // Output: // 99,73,85,66 }
Explode
[classA_tom classA_lily classA_john] }
命名转换
CaseCamel
说明:
CaseCamel
将字符串转换为大驼峰形式(首字母大写)。说明:Explode
使用分隔符delimiter
字符串str
拆分成[]string
格式:
Code Block language go Explode(delimiter, strCaseCamel(s string) []string
示例:
Code Block language go func ExampleExplodeExampleCaseCamel() { var ( str = `Hello`hello World`world` delimiter = " " result = gstr.ExplodeCaseCamel(delimiter, str) ) fmt.PrintfPrintln(`%#v`, result) // Output: // []string{"Hello", "World"}HelloWorld }
Implode
CaseCamelLower
说明:
Implode
使用glue
连接pieces
字符串数组的每一个元素。CaseCamelLower
将字符串转换为小驼峰形式(首字母小写)。格式:
Code Block language go ImplodeCaseCamelLower(glues string, pieces []string) string
示例:
Code Block language go func ExampleImplodeExampleCaseCamelLower() { var ( pieces = []string{"goframe", "is", "very", "easy", "to", "use"} gluestr = "`hello "world` result = gstr.Implode(glue, piecesCaseCamelLower(str) ) fmt.Println(result) // Output: // goframe is very easy to usehelloWorld }
Chr
CaseSnake
说明:
CaseSnake
将字符串转换中的符号(下划线,空格,点,中横线)用下划线(_
)替换,并全部转换为小写字母。说明:Chr
返回一个数字0-255
对应的ascii
字符串。格式:
Code Block language go ChrCaseSnake(asciis intstring) string
示例:
Code Block language go func ExampleChrExampleCaseSnake() { var ( asciistr = 65 // A`hello world` result = gstr.ChrCaseSnake(asciistr) ) fmt.Println(result) // Output: // Ahello_world }
Ord
CaseSnakeScreaming
说明:
CaseSnakeScreaming
把字符串中的符号(下划线,空格,点,中横线),全部替换为下划线'_'
,并将所有英文字母转为大写。说明:Ord
将字符串的第一个字节转换为0-255
之间的值。格式:
Code Block language go OrdCaseSnakeScreaming(chars string) intstring
示例:
Code Block language go func ExampleOrdExampleCaseSnakeScreaming() { var ( str = `goframe``hello world` result = gstr.OrdCaseSnakeScreaming(str) ) fmt.Println(result) // Output: // 103HELLO_WORLD }
HideStr
CaseSnakeFirstUpper
说明:
CaseSnakeFirstUpper
将字符串中的字母为大写时,将大写字母转换为小写字母并在其前面增加一个下划线'_'
,首字母大写时,只转换为小写,前面不增加下划线'_'
。说明:HideStr
将字符串str
从中间字符开始,百分比percent
的字符转换成hide
字符串。格式:
Code Block language go HideStrCaseSnakeFirstUpper(strword string, percent int, hide underscore ...string) string
示例:
Code Block language go func ExampleHideStrExampleCaseSnakeFirstUpper() { var ( str = `13800138000` percent = 40 hide = `*``RGBCodeMd5` result = gstr.HideStrCaseSnakeFirstUpper(str, percent, hide) ) fmt.Println(result) // Output: // 138****8000rgb_code_md5 }
Nl2Br
CaseKebab
说明:
Nl2Br
在字符串中的所有换行符之前插入HTML
换行符(' br ' |<br />): \n\r, \r\n, \r, \n
。CaseKebab
将字符串转换中的符号(下划线,空格,点,)用中横线'-'
替换,并全部转换为小写字母。格式:
Code Block language go Nl2BrCaseKebab(strs string, isXhtml ...bool) string
示例:
Code Block language go func ExampleNl2BrExampleCaseKebab() { var ( str = `hello `goframe is very easy to use` world` result = gstr.Nl2BrCaseKebab(str) ) fmt.Println(result) // Output: // goframe<br>is<br>very<br>easy<br>to<br>usehello-world }
AddSlashes
CaseKebabScreaming
说明:
AddSlashes
将字符串中的符号前添加转义字符'\'
CaseKebabScreaming
将字符串转换中的符号(下划线,空格,点,中横线)用中横线'-'
替换,并全部转换为大写字母。格式:
Code Block language go AddSlashesCaseKebabScreaming(strs string) string
示例:
Code Block language go func ExampleAddSlashesExampleCaseKebabScreaming() { var ( str = `'aa'"bb"cc\r\n\d\t``hello world` result = gstr.AddSlashesCaseKebabScreaming(str) ) fmt.Println(result) // Output: // \'aa\'\"bb\"cc\\r\\n\\d\\tHELLO-WORLD }
StripSlashes
CaseDelimited
说明:
StripSlashes
去掉字符串str
中的转义字符'\'
。CaseDelimited
将字符串转换中的符号进行替换。格式:
Code Block language go StripSlashesCaseDelimited(strs string, del byte) string
示例:
Code Block language go func ExampleStripSlashesExampleCaseDelimited() { var ( str = `hello world` del = `C:\\windows\\GoFrame\\test`byte('-') result = gstr.StripSlashesCaseDelimited(str, del) ) fmt.Println(result) // Output: // C:\windows\GoFrame\testhello-world }
QuoteMeta
CaseDelimitedScreaming
说明:
QuoteMeta
为str中'. \ + * ? [ ^ ] ( $ )
中的每个字符前添加一个转义字符'\'。
CaseDelimitedScreaming
将字符串中的符号(空格,下划线,点,中横线)用第二个参数进行替换,该函数第二个参数为替换的字符,第三个参数为大小写转换,true
为全部转换大写字母,false
为全部转为小写字母。格式:
Code Block language go QuoteMetaCaseDelimitedScreaming(strs string, chars ...stringdel uint8, screaming bool) string
示例:
Code Block language go func ExampleQuoteMetaExampleCaseDelimitedScreaming() { { var ( str = `hello world` del = `.\+?[^]()`byte('-') result = gstr.QuoteMetaCaseDelimitedScreaming(str, del, true) ) fmt.Println(result) } { var ( str = `https://goframe.org/pages/viewpage.action?pageId=1114327` `hello world` del = byte('-') result = gstr.QuoteMetaCaseDelimitedScreaming(str, del, false) ) fmt.Println(result) } // Output: // \.\\\+\?\[\^\]\(\)HELLO-WORLD // https://goframe\.org/pages/viewpage\.action\?pageId=1114327 }
Count
hello-world }
包含判断
Contains
说明:
Contains
返回字符串str
是否包含子字符串substr
,区分大小写。说明:格式:
Code Block language go Contains(str, substr string) bool
示例:
Code Block language go
AddSlashes
- 格式: AddSlashes
- 说明: 在英文的 单引号( ' ) , 双引号( " ), 反斜杠( \ ) 前面自动添加一个转义符号 反斜杠( \ )。
示例:
Code Block language go func ExampleAddSlashes() { var str string str = `'aa'"bb"cc\r\n\d\t` rsStr := gstr.AddSlashes(str) fmt.Println(rsStr) //func ExampleContains() { { var ( str = `Hello World` substr = `Hello` result = gstr.Contains(str, substr) ) fmt.Println(result) } { var ( str = `Hello World` substr = `hello` result = gstr.Contains(str, substr) ) fmt.Println(result) } // Output: // \'aa\'\"bb\"cc\\r\\n\\d\\ttrue // false }
CaseCamel
ContainsI
说明:
ContainsI
校验substr
是否在str
中,不区分大小写。格式:
Code Block language go ContainsI(str, substr string) bool
示例:
Code Block language go func ExampleContainsI() { var ( str = `Hello World` substr = "hello" result1 = gstr.Contains(str, substr) result2 = gstr.ContainsI(str, substr) ) fmt.Println(result1) fmt.Println(result2) // Output: // false // true }
ContainsAny
说明:
ContainsAny
校验s
中是否包含chars
。格式:
Code Block language go ContainsAny(s, chars string) bool
示例:
Code Block language go func ExampleContainsAny() { { var ( s = `goframe` chars = "g" result = gstr.ContainsAny(s, chars) ) fmt.Println(result) } { var ( s = `goframe` chars = "G" result = gstr.ContainsAny(s, chars) ) fmt.Println(result) } // Output: // true // false }
字符串转换
Chr
说明:
Chr
返回一个数字0-255
对应的ascii
字符串。格式:
Code Block language go Chr(ascii int) string
示例:
Code Block language go func ExampleChr() { var ( ascii = 65 // A result = gstr.Chr(ascii) ) fmt.Println(result) // Output: // A }
Ord
说明:
Ord
将字符串的第一个字节转换为0-255
之间的值。格式:
Code Block language go Ord(char string) int
示例:
Code Block language go func ExampleOrd() { var ( str = `goframe` result = gstr.Ord(str) ) fmt.Println(result) // Output: // 103 }
OctStr
说明:
OctStr
将字符串str
中的八进制字符串转换为其原始字符串。格式:
Code Block language go OctStr(str string) string
示例:
Code Block language go func ExampleOctStr() { var ( str = `\346\200\241` result = gstr.OctStr(str) ) fmt.Println(result) // Output: // 怡 }
Reverse
说明:
Reverse
返回str
的反转字符串。格式:
Code Block language go Reverse(str string) string
示例:
Code Block language go func ExampleReverse() { var ( str = `123456` result = gstr.Reverse(str) ) fmt.Println(result) // Output: // 654321 }
NumberFormat
说明:
NumberFormat
以千位分组来格式化数字。- 参数
decimal
设置小数点的个数。 - 参数
decPoint
设置小数点的分隔符。 参数
thousand
设置千位分隔符。
- 参数
格式:
Code Block language go NumberFormat(number float64, decimals int, decPoint, thousandsSep string) string
示例:
Code Block language go func ExampleNumberFormat() { var ( number float64 = 123456 decimals = 2 decPoint = "." thousandsSep = "," result = gstr.NumberFormat(number, decimals, decPoint, thousandsSep) ) fmt.Println(result) // Output: // 123,456.00 }
Shuffle
说明:
Shuffle
返回将str
随机打散后的字符串。格式:
Code Block language go Shuffle(str string) string
示例:
Code Block language go func ExampleShuffle() { var ( str = `123456` result = gstr.Shuffle(str) ) fmt.Println(result) // May Output: // 563214 }
HideStr
说明:
HideStr
将字符串str
从中间字符开始,百分比percent
的字符转换成hide
字符串。格式:
Code Block language go HideStr(str string, percent int, hide string) string
示例:
Code Block language go func ExampleHideStr() { var ( str = `13800138000` percent = 40 hide = `*` result = gstr.HideStr(str, percent, hide) ) fmt.Println(result) // Output: // 138****8000 }
Nl2Br
说明:
Nl2Br
在字符串中的所有换行符之前插入HTML
换行符(' br ' |<br />): \n\r, \r\n, \r, \n
。格式:
Code Block language go Nl2Br(str string, isXhtml ...bool) string
示例:
Code Block language go func ExampleNl2Br() { var ( str = `goframe is very easy to use` result = gstr.Nl2Br(str) ) fmt.Println(result) // Output: // goframe<br>is<br>very<br>easy<br>to<br>use }
WordWrap
说明:
WordWrap
使用换行符将str
换行到给定字符数(不会切分单词)。格式:
Code Block language go WordWrap(str string, width int, br string) string
示例:
Code Block language go func ExampleWordWrap() { { var ( str = `A very long woooooooooooooooooord. and something` width = 8 br = "\n" result = gstr.WordWrap(str, width, br) ) fmt.Println(result) } { var ( str = `The quick brown fox jumped over the lazy dog.` width = 20 br = "<br />\n" result = gstr.WordWrap(str, width, br) ) fmt.Printf("%v", result) } // Output: // A very // long // woooooooooooooooooord. // and // something // The quick brown fox<br /> // jumped over the lazy<br /> // dog. }
域名处理
IsSubDomain
说明:
IsSubDomain
校验subDomain
是否为mainDomain
的子域名。 支持mainDomain
中的'*'
。格式:
Code Block language go IsSubDomain(subDomain string, mainDomain string) bool
示例:
Code Block language go func ExampleIsSubDomain() { var ( subDomain = `s.goframe.org` mainDomain = `goframe.org` result = gstr.IsSubDomain(subDomain, mainDomain) ) fmt.Println(result) // Output: // true }
参数解析
Parse
说明:
Parse
解析字符串并以map[string]interface{}
类型返回。格式:
Code Block language go Parse(s string) (result map[string]interface{}, err error)
示例:
Code Block language go func ExampleParse() { { var ( str = `v1=m&v2=n` result, _ = gstr.Parse(str) ) fmt.Println(result) } { var ( str = `v[a][a]=m&v[a][b]=n` result, _ = gstr.Parse(str) ) fmt.Println(result) } { // The form of nested Slice is not yet supported. var str = `v[][]=m&v[][]=n` result, err := gstr.Parse(str) if err != nil { panic(err) } fmt.Println(result) } { // This will produce an error. var str = `v=m&v[a]=n` result, err := gstr.Parse(str) if err != nil { println(err) } fmt.Println(result) } { var ( str = `a .[[b=c` result, _ = gstr.Parse(str) ) fmt.Println(result) } // May Output: // map[v1:m v2:n] // map[v:map[a:map[a:m b:n]]] // map[v:map[]] // Error: expected type 'map[string]interface{}' for key 'v', but got 'string' // map[] // map[a___[b:c] }
位置查找
Pos
说明:
Pos
返回needle
在haystack
中第一次出现的位置,区分大小写。 如果没有找到,则返回-1。格式:
Code Block language go Pos(haystack, needle string, startOffset ...int) int
示例:
Code Block language go func ExamplePos() { var ( haystack = `Hello World` needle = `World` result = gstr.Pos(haystack, needle) ) fmt.Println(result) // Output: // 6 }
PosRune
说明:
PosRune
的作用于函数Pos
相似,但支持haystack
和needle
为unicode
字符串。格式:
Code Block language go PosRune(haystack, needle string, startOffset ...int) int
示例:
Code Block language go func ExamplePosRune() { var ( haystack = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架` needle = `Go` posI = gstr.PosRune(haystack, needle) posR = gstr.PosRRune(haystack, needle) ) fmt.Println(posI) fmt.Println(posR) // Output: // 0 // 22 }
PosI
说明:
PosI
返回needle
在haystack
中第一次出现的位置,不区分大小写。 如果没有找到,则返回-1。格式:
Code Block language go PosI(haystack, needle string, startOffset ...int) int
示例:
Code Block language go func ExamplePosI() { var ( haystack = `goframe is very, very easy to use` needle = `very` posI = gstr.PosI(haystack, needle) posR = gstr.PosR(haystack, needle) ) fmt.Println(posI) fmt.Println(posR) // Output: // 11 // 17 }
PosRuneI
说明:
PosRuneI
的作用于函数PosI
相似,但支持haystack
和needle
为unicode
字符串。格式:
Code Block language go PosIRune(haystack, needle string, startOffset ...int) int
示例:
Code Block language go func ExamplePosIRune() { { var ( haystack = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架` needle = `高性能` startOffset = 10 result = gstr.PosIRune(haystack, needle, startOffset) ) fmt.Println(result) } { var ( haystack = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架` needle = `高性能` startOffset = 30 result = gstr.PosIRune(haystack, needle, startOffset) ) fmt.Println(result) } // Output: // 14 // -1 }
PosR
说明:
PosR
返回needle
在haystack
中最后一次出现的位置,区分大小写。 如果没有找到,则返回-1。格式:
Code Block language go PosR(haystack, needle string, startOffset ...int) int
示例:
Code Block language go func ExamplePosR() { var ( haystack = `goframe is very, very easy to use` needle = `very` posI = gstr.PosI(haystack, needle) posR = gstr.PosR(haystack, needle) ) fmt.Println(posI) fmt.Println(posR) // Output: // 11 // 17 }
PosRuneR
说明:
PosRuneR
的作用于函数PosR
相似,但支持haystack
和needle
为unicode
字符串。格式:
Code Block language go PosRRune(haystack, needle string, startOffset ...int) int
示例:
Code Block language go func ExamplePosRRune() { var ( haystack = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架` needle = `Go` posI = gstr.PosIRune(haystack, needle) posR = gstr.PosRRune(haystack, needle) ) fmt.Println(posI) fmt.Println(posR) // Output: // 0 // 22 }
PosRI
说明:
PosRI
返回needle
在haystack
中最后一次出现的位置,不区分大小写。 如果没有找到,则返回-1。格式:
Code Block language go PosRI(haystack, needle string, startOffset ...int) int
示例:
Code Block language go func ExamplePosRI() { var ( haystack = `goframe is very, very easy to use` needle = `VERY` posI = gstr.PosI(haystack, needle) posR = gstr.PosRI(haystack, needle) ) fmt.Println(posI) fmt.Println(posR) // Output: // 11 // 17 }
PosRIRune
说明:
PosRIRune
的作用于函数PosRI
相似,但支持haystack
和needle
为unicode
字符串。格式:
Code Block language go PosRIRune(haystack, needle string, startOffset ...int) int
示例:
Code Block language go func ExamplePosRIRune() { var ( haystack = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架` needle = `GO` posI = gstr.PosIRune(haystack, needle) posR = gstr.PosRIRune(haystack, needle) ) fmt.Println(posI) fmt.Println(posR) // Output: // 0 // 22 }
查找替换
Replace
说明:
Replace
返回origin
字符串中,search
被replace
替换后的新字符串。search
区分大小写。格式:
Code Block language go Replace(origin, search, replace string, count ...int) string
示例:
Code Block language go func ExampleReplace() { var ( origin = `golang is very nice!` search = `golang` replace = `goframe` result = gstr.Replace(origin, search, replace) ) fmt.Println(result) // Output: // goframe is very nice! }
ReplaceI
说明:
ReplaceI
返回origin
字符串中,search
被replace
替换后的新字符串。search
不区分大小写。格式:
Code Block language go ReplaceI(origin, search, replace string, count ...int) string
示例:
Code Block language go func ExampleReplaceI() { var ( origin = `golang is very nice!` search = `GOLANG` replace = `goframe` result = gstr.ReplaceI(origin, search, replace) ) fmt.Println(result) // Output: // goframe is very nice! }
ReplaceByArray
说明:
ReplaceByArray
返回origin
被一个切片按两个一组(search, replace)
顺序替换的新字符串,区分大小写。格式:
Code Block language go ReplaceByArray(origin string, array []string) string
示例:
Code Block language go func ExampleReplaceByArray() { { var ( origin = `golang is very nice` array = []string{"lang", "frame"} result = gstr.ReplaceByArray(origin, array) ) fmt.Println(result) } { var ( origin = `golang is very good` array = []string{"golang", "goframe", "good", "nice"} result = gstr.ReplaceByArray(origin, array) ) fmt.Println(result) } // Output: // goframe is very nice // goframe is very nice }
ReplaceIByArray
说明:
ReplaceIByArray
返回origin
被一个切片按两个一组(search, replace)
顺序替换的新字符串,不区分大小写。格式:
Code Block language go ReplaceIByArray(origin string, array []string) string
示例:
Code Block language go func ExampleReplaceIByArray() { var ( origin = `golang is very Good` array = []string{"Golang", "goframe", "GOOD", "nice"} result = gstr.ReplaceIByArray(origin, array) ) fmt.Println(result) // Output: // goframe is very nice }
ReplaceByMap
说明:
ReplaceByMap
返回origin
中map
的key
替换为value
的新字符串,区分大小写。格式:
Code Block language go ReplaceByMap(origin string, replaces map[string]string) string
示例:
Code Block language go func ExampleReplaceByMap() { { var ( origin = `golang is very nice` replaces = map[string]string{ "lang": "frame", } result = gstr.ReplaceByMap(origin, replaces) ) fmt.Println(result) } { var ( origin = `golang is very good` replaces = map[string]string{ "golang": "goframe", "good": "nice", } result = gstr.ReplaceByMap(origin, replaces) ) fmt.Println(result) } // Output: // goframe is very nice // goframe is very nice }
ReplaceIByMap
说明:
ReplaceIByMap
返回origin
中map
的key
替换为value
的新字符串,不区分大小写。格式:
Code Block language go ReplaceIByMap(origin string, replaces map[string]string) string
示例:
Code Block language go func ExampleReplaceIByMap() { var ( origin = `golang is very nice` replaces = map[string]string{ "Lang": "frame", } result = gstr.ReplaceIByMap(origin, replaces) ) fmt.Println(result) // Output: // goframe is very nice }
子串截取
Str
说明:
Str
返回从needle
第一次出现的位置开始,到haystack
结尾的字符串(包含needle
本身)。格式:
Code Block language go Str(haystack string, needle string) string
示例:
Code Block language go func ExampleStr() { var ( haystack = `xxx.jpg` needle = `.` result = gstr.Str(haystack, needle) ) fmt.Println(result) // Output: // .jpg }
StrEx
说明:
StrEx
返回从needle
第一次出现的位置开始,到haystack
结尾的字符串(不包含needle
本身)。格式:
Code Block language go StrEx(haystack string, needle string) string
示例:
Code Block language go func ExampleStrEx() { var ( haystack = `https://goframe.org/index.html?a=1&b=2` needle = `?` result = gstr.StrEx(haystack, needle) ) fmt.Println(result) // Output: // a=1&b=2 }
StrTill
说明:
StrTill
返回从haystack
字符串开始到needle
第一次出现的位置的字符串(包含needle
本身)。格式:
Code Block language go StrTill(haystack string, needle string) string
示例:
Code Block language go func ExampleStrTill() { var ( haystack = `https://goframe.org/index.html?test=123456` needle = `?` result = gstr.StrTill(haystack, needle) ) fmt.Println(result) // Output: // https://goframe.org/index.html? }
StrTillEx
说明:
StrTillEx
返回从haystack
字符串开始到needle
第一次出现的位置的字符串(不包含needle
本身)。格式:
Code Block language go StrTillEx(haystack string, needle string) string
示例:
Code Block language go func ExampleStrTillEx() { var ( haystack = `https://goframe.org/index.html?test=123456` needle = `?` result = gstr.StrTillEx(haystack, needle) ) fmt.Println(result) // Output: // https://goframe.org/index.html }
SubStr
说明:
SubStr
返回字符串str
从start
开始,长度为length
的新字符串。 参数length
是可选的,它默认使用str
的长度。格式:
Code Block language go SubStr(str string, start int, length ...int) (substr string)
示例:
Code Block language go func ExampleSubStr() { var ( str = `1234567890` start = 0 length = 4 subStr = gstr.SubStr(str, start, length) ) fmt.Println(subStr) // Output: // 1234 }
SubStrRune
说明:
SubStrRune
返回unicode
字符串str
从start
开始,长度为length
的新字符串。 参数length
是可选的,它默认使用str
的长度。格式:
Code Block language go SubStrRune(str string, start int, length ...int) (substr string)
示例:
Code Block language go func ExampleSubStrRune() { var ( str = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架。` start = 14 length = 3 subStr = gstr.SubStrRune(str, start, length) ) fmt.Println(subStr) // Output: // 高性能 }
StrLimit
说明:
StrLimit
取str
字符串开始,长度为length
的字符串,加上suffix...
后返回新的字符串。格式:
Code Block language go StrLimit(str string, length int, suffix ...string) string
示例:
Code Block language go func ExampleStrLimit() { var ( str = `123456789` length = 3 suffix = `...` result = gstr.StrLimit(str, length, suffix) ) fmt.Println(result) // Output: // 123... }
StrLimitRune
说明:
StrLimitRune
取unicode
字符串str
开始,长度为length
的字符串,加上suffix...
后返回新的字符串。格式:
Code Block language go StrLimitRune(str string, length int, suffix ...string) string
示例:
Code Block language go func ExampleStrLimitRune() { var ( str = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架。` length = 17 suffix = "..." result = gstr.StrLimitRune(str, length, suffix) ) fmt.Println(result) // Output: // GoFrame是一款模块化、高性能... }
SubStrFrom
说明:
SubStrFrom
返回字符串str
从第一次出现need
到str
的结尾的字符串(包含need
)。格式:
Code Block language go SubStrFrom(str string, need string) (substr string)
示例:
Code Block language go func ExampleSubStrFrom() { var ( str = "我爱GoFrameGood" need = `爱` ) fmt.Println(gstr.SubStrFrom(str, need)) // Output: // 爱GoFrameGood }
SubStrFromEx
说明:
SubStrFromEx
返回字符串str
从第一次出现need
到str
的结尾的字符串(不包含need
)。格式:
Code Block language go SubStrFromEx(str string, need string) (substr string)
示例:
Code Block language go func ExampleSubStrFromEx() { var ( str = "我爱GoFrameGood" need = `爱` ) fmt.Println(gstr.SubStrFromEx(str, need)) // Output: // GoFrameGood }
SubStrFromR
说明:
SubStrFromR
返回字符串str
从最后一次出现need
到str
的结尾的字符串(包含need
)。格式:
Code Block language go SubStrFromR(str string, need string) (substr string)
示例:
Code Block language go func ExampleSubStrFromR() { var ( str = "我爱GoFrameGood" need = `Go` ) fmt.Println(gstr.SubStrFromR(str, need)) // Output: // Good }
SubStrFromREx
说明:
SubStrFromREx
返回字符串str
从最后一次出现need
到str
的结尾的字符串(不包含need
)。格式:
Code Block language go SubStrFromREx(str string, need string) (substr string)
示例:
Code Block language go func ExampleSubStrFromREx() { var ( str = "我爱GoFrameGood" need = `Go` ) fmt.Println(gstr.SubStrFromREx(str, need)) // Output: // od }
字符/子串过滤
Trim
说明:
Trim
从字符串的开头和结尾剪切空白(或其他字符)。 可选参数characterMask
指定额外剥离的字符。格式:
Code Block language go Trim(str string, characterMask ...string) string
示例:
Code Block language go func ExampleTrim() { var ( str = `*Hello World*` characterMask = "*d" result = gstr.Trim(str, characterMask) ) fmt.Println(result) // Output: // Hello Worl }
TrimStr
说明:
TrimStr
从字符串的开头和结尾去掉所有cut
字符串(不会删除开头或结尾的空白)。格式:
Code Block language go TrimStr(str string, cut string, count ...int) string
示例:
Code Block language go func ExampleTrimStr() { var ( str = `Hello World` cut = "World" count = -1 result = gstr.TrimStr(str, cut, count) ) fmt.Println(result) // Output: // Hello }
TrimLeft
说明:
TrimLeft
将字符串开头的空格(或其他字符)删除。格式:
Code Block language go TrimLeft(str string, characterMask ...string) string
示例:
Code Block language go func ExampleTrimLeft() { var ( str = `*Hello World*` characterMask = "*" result = gstr.TrimLeft(str, characterMask) ) fmt.Println(result) // Output: // Hello World* }
TrimLeftStr
说明:
TrimLeftStr
从字符串的开头删除count
个cut
字符串(不会删除开头的空格)。格式:
Code Block language go TrimLeftStr(str string, cut string, count ...int) string
示例:
Code Block language go func ExampleTrimLeftStr() { var ( str = `**Hello World**` cut = "*" count = 1 result = gstr.TrimLeftStr(str, cut, count) ) fmt.Println(result) // Output: // *Hello World** }
TrimRight
说明:
TrimRight
从字符串的末尾去掉空白(或其他字符)。格式:
Code Block language go TrimRight(str string, characterMask ...string) string
示例:
Code Block language go func ExampleTrimRight() { var ( str = `**Hello World**` characterMask = "*def" // []byte{"*", "d", "e", "f"} result = gstr.TrimRight(str, characterMask) ) fmt.Println(result
- 格式: CaseCamel
- 说明: 将字符串转换为大驼峰形式(首字母大写)。
示例: `goframe_is very nice.to-use` 字符串中的下划线,空格,点,中横杠都可以作为驼峰的分割点。
Code Block language go func ExampleCaseCamel() { var str string str = `goframe_is very nice.to-use` rsStr := gstr.CaseCamel(str) fmt.Println(rsStr) // Output: // **Hello GoframeIsVeryNiceToUseWorl }
CaseCamelLower
TrimRightStr
说明:
TrimRightStr
从字符串的尾部删除count
个cut
字符串(不会删除尾部的空格)。格式:
Code Block language go TrimRightStr(str string, cut string, count ...int) string
示例:
- 格式: CaseCamelLower
- 说明: 将字符串转换为小驼峰形式(首字母小写)。
示例: `goframe_is very nice.to-use` 字符串中的下划线,空格,点,中横杠都可以作为驼峰的分割点。
Code Block language go func ExampleCaseCamelLowerExampleTrimRightStr() { var ( str string str = `goframe_is very nice.to-use` rsStr : = `Hello World!` cut = "!" count = -1 result = gstr.CaseCamelLowerTrimRightStr(str, cut, count) ) fmt.Println(rsStrresult) // Output: // goframeIsVeryNiceToUse }
CaseDelimited
Hello World }
TrimAll
说明:
TrimAll
删除字符串str
中的所有空格(或其他字符)以及characterMask
字符。格式:
Code Block language go TrimAll(str string, characterMask ...string) string
示例:
- 格式: CaseDelimited
- 说明: 将字符串转换中的符号进行替换,该函数第二个参数为替换的uint8类型字符。
示例: del参数为int8类型的35,就是字符 # (井号)
Code Block language go func ExampleCaseDelimitedExampleTrimAll() { var ( str string var del uint8 str = `goframe_is_very-nice.to-use` del = 35 rsStr := gstr.CaseDelimited(str,del = `*Hello World*` characterMask = "*" result = gstr.TrimAll(str, characterMask) ) fmt.Println(rsStrresult) // Output: // goframe#is#very#nice#to#useHelloWorld }
CaseDelimitedScreaming
HasPrefix
说明:
HasPrefix
返回s
是否以prefix
开头。格式:
Code Block language go HasPrefix(s, prefix string) bool
示例:
- 格式: CaseDelimitedScreaming
- 说明: 将字符串中的符号(空格,下划线,点,中横线)用第二个参数进行替换,该函数第二个参数为替换的uint8类型字符,第三个参数为大小写转换,true为全部转换大写字母,false为全部转为小写字母。
示例: del参数为int8类型的35,就是字符 # (井号)
Code Block language go func ExampleCaseDelimitedScreamingExampleHasPrefix() { var str string( var s del uint8 var screaming bool str = `goframe_is Very Nice.to-use` del = 35 screaming`Hello World` prefix = true"Hello" rsStr result := gstr.CaseDelimitedScreamingHasPrefix(str,del,screamings, prefix) ) fmt.Println(rsStrresult) // Output: // GOFRAME#IS#VERY#NICE#TO#USEtrue }
CaseKebab
HasSuffix
说明:
HasSuffix
返回s
是否以suffix
结束。格式:
Code Block language go HasSuffix(s, suffix string) bool
示例:
- 格式: CaseKebab
- 说明: 将字符串转换中的符号(下划线,空格,点,)用中横线(-)替换,并全部转换为小写字母。
示例:
Code Block language go func ExampleCaseKebabExampleHasSuffix() { var str string( str s = `goframe_is Very Nice.to-use` rsStr :`my best love is goframe` prefix = "goframe" result = gstr.CaseKebab(strHasSuffix(s, prefix) ) fmt.Println(rsStrresult) // Output: // goframe-is-very-nice-to-use }
CaseKebabScreaming
true }
版本比较
CompareVersion
说明:
CompareVersion
将a
和b
作为标准GNU
版本进行比较。格式:
Code Block language go CompareVersion(a, b string) int
示例:
- 格式: CaseKebabScreaming
- 说明: 将字符串转换中的符号(下划线,空格,点,中横线)用中横线(-)替换,并全部转换为大写字母。
示例:
Code Block language go func ExampleCaseKebabScreamingExampleCompareVersion() { var str string str = `goframe_is Very Nice.to-use` rsStr := gstr.CaseKebabScreaming(str { fmt.Println(gstr.CompareVersion("v2.11.9", "v2.10.8")) fmt.Println(gstr.CompareVersion("1.10.8", "1.19.7")) fmt.Println(rsStrgstr.CompareVersion("2.8.beta", "2.8")) // Output:: // 1 // -1 // GOFRAME-IS-VERY-NICE-TO-USE0 }
CaseSnake
CompareVersionGo
说明:
CompareVersionGo
将a
和b
作为标准的Golang
版本进行比较。格式:
Code Block language go CompareVersionGo(a, b string) int
示例:
- 格式: CaseSnake
- 说明: 将字符串转换中的符号(下划线,空格,点,中横线)用下划线( _ )替换,并全部转换为小写字母。
示例:
Code Block language go func ExampleCaseSnakeExampleCompareVersionGo() { var str string str = `goframe_is Very Nice.to-use` rsStr := gstr.CaseSnake(strfmt.Println(gstr.CompareVersionGo("v2.11.9", "v2.10.8")) fmt.Println(gstr.CompareVersionGo("v4.20.1", "v4.20.1+incompatible")) fmt.Println(rsStr(gstr.CompareVersionGo( "v0.0.2-20180626092158-b2ccc119800e", "v1.0.1-20190626092158-b2ccc519800e", )) // Output: // goframe_is_very_nice_to_use }
CaseSnakeFirstUpper
1 // 1 // -1 }
相似计算
Levenshtein
说明:
Levenshtein
计算两个字符串之间的Levenshtein
距离。格式:
Code Block language go Levenshtein(str1, str2 string, costIns, costRep, costDel int) int
示例:
Code Block language go func ExampleLevenshtein() { var ( str1 = "Hello World" str2 = "hallo World" costIns = 1 costRep = 1 costDel = 1 result = gstr.Levenshtein(str1, str2, costIns, costRep, costDel)
- 格式: CaseSnakeFirstUpper
- 说明: 当字符串中的字母为大写时,将大写字母转换为小写字母并在其前面增加一个下划线( _ ),首字母大写时,只转换为小写,前面不增加下划线( _ )。
示例:
Code Block language go func ExampleCaseSnakeFirstUpper() { var str string str = `GoframeIsVeryNiceToUse` rsStr := gstr.CaseSnakeFirstUpper(str) fmt.Println(rsStrresult) // Output: // goframe_is_very_nice_to_use2 }
CaseSnakeScreaming
SimilarText
说明:
SimilarText
计算两个字符串之间的相似度。格式:
Code Block language go SimilarText(first, second string, percent *float64) int
示例:
- 格式: CaseSnakeScreaming
- 说明: 当字符串中的符号(下划线,空格,点,中横线),全部替换为下划线( _ ),并将所有英文字母转为大写。
示例:
Code Block language go func ExampleCaseSnakeScreamingExampleSimilarText() { var str string str = `goframe_is Very Nice.to-use` rsStr :( first = `AaBbCcDd` second = `ad` percent = 0.80 result = gstr.CaseSnakeScreaming(strSimilarText(first, second, &percent) ) fmt.Println(rsStrresult) // Output: // GOFRAME_IS_VERY_NICE_TO_USE2 }
Chr
Soundex
说明:
Soundex
用于计算字符串的Soundex
键。格式:
Code Block language go Soundex(str string) string
示例:
- 格式: Chr
- 说明: 将ASCII码表中对应的十进制数字转换为字符, 其 ASCII码 (American Standard Code for Information Interchange) 为 美国信息交换标准代码,
示例: 在ASCII表中英文大写字母A对应的十进制数为65
Code Block language go func ExampleChrExampleSoundex() { var ascii int ascii = 65 rsStr :( str1 = `Hello` str2 = `Hallo` result1 = gstr.Soundex(str1) result2 = gstr.Chr(asciiSoundex(str2) ) fmt.Println(rsStrresult1, result2) // Output: // H400 AH400 }
Count
说明:
格式:
Code Block language go 示例:
Code Block language go
Panel | ||
---|---|---|
| ||
|