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://pkg.go.dev/github.com/gogf/gf/v2/text/gstr
Tip |
---|
以下常用方法列表,文档更新可能滞后于代码新特性,更多的方法及示例请参考代码文档:https://pkg.go.dev/github.com/gogf/gf/v2/text/gstr |
字符串判断
IsNumeric
说明:
IsNumeric
验证字符串s
是否为数字。格式:
Code Block language go IsNumeric(s string) bool
示例:
Code Block language go func ExampleIsNumeric() { fmt.Println(gstr.IsNumeric("88")) fmt.Println(gstr.IsNumeric("3.1415926")) fmt.Println(gstr.IsNumeric("abc")) // Output: // true // true // false }
字符串长度
Reverse
LenRune
说明:
Reverse
返回str
的反转字符串。LenRune
返回unicode
字符串长度。格式:
Code Block language go ReverseLenRune(str string) stringint
示例:
Code Block language go func ExampleReverseExampleLenRune() { var ( str = `123456``GoFrame框架` result = gstr.ReverseLenRune(str) ) fmt.Println(result) // Output: // 6543219 }
字符串创建
NumberFormat
Repeat
说明:
Repeat
返回一个由input
重复multiplier
次后组成的新字符串。说明:
NumberFormat
以千位分组来格式化数字。- 参数
decimal
设置小数点的个数。 - 参数
decPoint
设置小数点的分隔符。 参数
thousand
设置千位分隔符。
- 参数
格式:
Code Block language go NumberFormatRepeat(numberinput float64string, decimalsmultiplier int, decPoint, thousandsSep string) string
示例:
Code Block language go func ExampleNumberFormatExampleRepeat() { var ( numberinput = float64`goframe = 123456` decimals multiplier = 23 decPointresult = "." thousandsSep = "," result = gstr.NumberFormat(number, decimals, decPoint, thousandsSepgstr.Repeat(input, multiplier) ) fmt.Println(result) // Output: // 123,456.00 goframe goframe goframe }
Fields
大小写转换
ToLower
说明:
ToLower
将s
中所有Unicode
字符都变为小写并返回其副本。 说明:Fields
以[]string
的形式返回字符串中的每个单词。格式:
Code Block language go FieldsToLower(strs string) []string
示例:
Code Block language go func ExampleFieldsExampleToLower() { var ( strs = `Hello World``GOFRAME` result = gstr.FieldsToLower(strs) ) fmt.PrintfPrintln(`%#v`, result) // Output: // []string{"Hello", "World"}goframe }
HasPrefix
ToUpper
说明:
HasPrefix
返回s
是否以prefix
开头。ToUpper
将s
中所有Unicode
字符都变为大写并返回其副本。格式:
Code Block language go HasPrefixToUpper(s, prefix string) boolstring
示例:
Code Block language go func ExampleHasPrefixExampleToUpper() { var ( s = `Hello World` prefix = "Hello"`goframe` result = gstr.HasPrefixToUpper(s, prefix) ) fmt.Println(result) // Output: // trueGOFRAME }
HasSuffix
UcFirst
说明:
HasSuffix
返回s
是否以suffix
结束。UcFirst
将s
中首字符变为大写并返回其副本。格式:
Code Block language go HasSuffixUcFirst(s, suffix string) boolstring
示例:
Code Block language go func ExampleHasSuffixExampleUcFirst() { var ( s = `my best love is goframe` prefix = "goframe"`hello` result = gstr.HasSuffixUcFirst(s, prefix) ) fmt.Println(result) // Output: // trueHello }
WordWrap
LcFirst
说明:
WordWrap
使用换行符将str
换行到给定字符数(不会切分单词)。LcFirst
将s
中首字符变为小写并返回其副本。格式:
Code Block language go WordWrapLcFirst(strs string, width int, br string) string
示例:
Code Block language go func ExampleWordWrapExampleLcFirst() { { var ( str = `A very long woooooooooooooooooord. and something` width = 8 br `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` = "\n" result = gstr.WordWrapUcWords(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. }
LenRune
说明:LenRune
返回unicode
字符串长度。
格式:
Code Block | ||
---|---|---|
| ||
LenRune(str string) int |
示例:
Code Block | ||
---|---|---|
| ||
func ExampleLenRune() {
var (
str = `GoFrame框架`
result = gstr.LenRune(str)
)
fmt.Println(result)
// Output:
// 9
} |
Repeat
说明:Repeat
返回一个由input
重复multiplier
次后组成的新字符串。
格式:
Code Block | ||
---|---|---|
| ||
Repeat(input string, multiplier int) string |
// Output: // Hello World }
IsLetterLower
说明:
IsLetterLower
验证给定的字符b
是否是小写字符。
示例:
Code Block | ||
---|---|---|
| ||
func ExampleRepeat() {
var (
input = `goframe `
multiplier = 3
result = gstr.Repeat(input, multiplier)
)
fmt.Println(result)
// Output:
// goframe goframe goframe
} |
Shuffle
说明:
Shuffle
返回将str
随机打散后的字符串。格式:
Code Block language go ShuffleIsLetterLower(strb stringbyte) stringbool
示例:
Code Block language go func ExampleShuffleExampleIsLetterLower() { var ( str = `123456` result = gstr.Shuffle(str) fmt.Println(gstr.IsLetterLower('a')) fmt.Println(resultgstr.IsLetterLower('A')) // May Output: // true // 563214false }
HideStr
IsLetterUpper
说明:
HideStr
将字符串str
从中间字符开始,百分比percent
的字符转换成hide
字符串。IsLetterUpper
验证字符b
是否是大写字符。格式:
Code Block language go HideStrIsLetterUpper(str string, percent int, hide stringb byte) stringbool
示例:
Code Block language go func ExampleHideStrExampleIsLetterUpper() { var ( str = `13800138000` percent = 40 hide = `*` result = gstr.HideStr(str, percent, hide) fmt.Println(gstr.IsLetterUpper('A')) fmt.Println(resultgstr.IsLetterUpper('a')) // Output: // true // 138****8000false }
Nl2Br
字符串比较
Compare
说明:
Compare
返回一个按字典顺序比较两个字符串的整数。 如果a == b
,结果为0
,如果a < b
,结果为-1
,如果a > b
,结果为+1
说明:Nl2Br
在字符串中的所有换行符之前插入HTML
换行符(' br ' |<br />): \n\r, \r\n, \r, \n
。格式:
Code Block language go Nl2BrCompare(str stringa, isXhtml ...boolb string) stringint
示例:
Code Block language go func ExampleNl2BrExampleCompare() { var ( str = `goframe is very easy to use` result = gstr.Nl2Br(str) ) fmt.Println(result) fmt.Println(gstr.Compare("c", "c")) fmt.Println(gstr.Compare("a", "b")) fmt.Println(gstr.Compare("c", "b")) // Output: // 0 // Output:-1 // goframe<br>is<br>very<br>easy<br>to<br>use1 }
大小写转换
ToLower
Equal
说明:
Equal
返回a
和b
在不区分大小写的情况下是否相等。说明:ToLower
将s
中所有Unicode
字符都变为小写并返回其副本。格式:
Code Block language go ToLower(sEqual(a, b string) stringbool
示例:
Code Block language go func ExampleToLowerExampleEqual() { var ( s = `GOFRAME` result = gstr.ToLower(s) fmt.Println(gstr.Equal(`A`, `a`)) fmt.Println(gstr.Equal(`A`, `A`)) fmt.Println(result(gstr.Equal(`A`, `B`)) // Output: // goframetrue // true // false }
ToUpper
切分组合
Split
说明:
ToUpper
将s
中所有Unicode
字符都变为大写并返回其副本。Split
用delimiter
将str
拆分为[]string
。格式:
Code Block language go ToUpper(sSplit(str, delimiter string) []string
示例:
Code Block language go func ExampleToUpperExampleSplit() { var ( sstr = `a|b|c|d` delimiter = `goframe``|` result = gstr.ToUpper(sSplit(str, delimiter) ) fmt.PrintlnPrintf(`%#v`, result) // Output: // GOFRAME[]string{"a", "b", "c", "d"} }
UcFirst
SplitAndTrim
说明:
UcFirst
将s
中首字符变为大写并返回其副本。SplitAndTrim
使用delimiter
将str
拆分为[]string
,并对[]string
的每个元素调用Trim
,并忽略在Trim
之后为空的元素。格式:
Code Block language go UcFirst(s SplitAndTrim(str, delimiter string, characterMask ...string) []string
示例:
Code Block language go func ExampleUcFirstExampleSplitAndTrim() { var ( sstr = `a|b|||||c|d` delimiter = `hello``|` result = gstr.UcFirst(sSplitAndTrim(str, delimiter) ) fmt.PrintlnPrintf(`%#v`, result) // Output: // Hello []string{"a", "b", "c", "d"} }
LcFirst
Join
说明:
LcFirst
将s
中首字符变为小写并返回其副本。Join
将array
中的每一个元素连接并生成一个新的字符串。参数sep
会作为新字符串的分隔符。格式:
Code Block language go LcFirst(sJoin(array []string, sep string) string
示例:
Code Block language go func ExampleLcFirst() { var ( str ExampleJoin() { var ( array = []string{"goframe", "is", "very", "easy", "to", "use"} sep = `Goframe`` ` result = gstr.LcFirst(strJoin(array, sep) ) fmt.Println(result) // Output: // goframe is very easy to use }
UcWords
JoinAny
说明:
UcWords
将字符串str
中每个单词的第一个字符变为大写。JoinAny
将array
中的每一个元素连接并生成一个新的字符串。参数sep
会作为新字符串的分隔符。参数array
可以是任意的类型。格式:
Code Block language go UcWords(strJoinAny(array interface{}, sep string) string
示例:
Code Block language go func ExampleUcWordsExampleJoinAny() { var ( str sep = `,` arr2 = `hello world`[]int{99, 73, 85, 66} result = gstr.UcWords(strJoinAny(arr2, sep) ) fmt.Println(result) // Output: // Hello World99,73,85,66 }
IsLetterLower
Explode
说明:
IsLetterLower
验证给定的字符b
是否是小写字符。Explode
使用分隔符delimiter
字符串str
拆分成[]string
格式:
Code Block language go IsLetterLower(b byte) boolExplode(delimiter, str string) []string
示例:
Code Block language go func ExampleIsLetterLowerExampleExplode() { fmt.Println(gstr.IsLetterLower('a')var ( str = `Hello World` delimiter = " " result = gstr.Explode(delimiter, str) ) fmt.Println(gstr.IsLetterLower('A')Printf(`%#v`, result) // Output: // true // false[]string{"Hello", "World"} }
IsLetterUpper
Implode
说明:
IsLetterUpper
验证字符b
是否是大写字符。Implode
使用glue
连接pieces
字符串数组的每一个元素。格式:
Code Block language go IsLetterUpper(b byte) boolImplode(glue string, pieces []string) string
示例:
Code Block language go func ExampleIsLetterUpperExampleImplode() { fmt.Println(gstr.IsLetterUpper('A')var ( pieces = []string{"goframe", "is", "very", "easy", "to", "use"} glue = " " result = gstr.Implode(glue, pieces) ) fmt.Println(gstr.IsLetterUpper('a')result) // Output: // true // false goframe is very easy to use }
ChunkSplit
Compare
说明:
Compare
返回一个按字典顺序比较两个字符串的整数。 如果a == b
,结果为0
,如果a < b
,结果为-1
,如果a > b
,结果为+1
。 说明:ChunkSplit
将字符串拆分为单位为chunkLen
长度更小的每一份,并用end
连接每一份拆分出的字符串。格式:
Code Block language go Compare(aChunkSplit(body string, chunkLen int, bend string) intstring
示例:
Code Block language go func ExampleCompareExampleChunkSplit() { fmt.Println(gstr.Compare("c", "c")) fmt.Println(gstr.Compare("a", "b")var ( body = `1234567890` chunkLen = 2 end = "#" result = gstr.ChunkSplit(body, chunkLen, end) ) fmt.Println(gstr.Compare("c", "b")result) // Output: // 0 // -1 // 1 12#34#56#78#90# }
Equal
Fields
说明:
Equal
返回a
和b
在不区分大小写的情况下是否相等。Fields
以[]string
的形式返回字符串中的每个单词。格式:
Code Block language go EqualFields(a,str b string) bool[]string
示例:
Code Block language go func ExampleEqualExampleFields() { fmt.Println(gstr.Equal(`A`, `a`)) fmt.Println(gstr.Equal(`A`, `A`)var ( str = `Hello World` result = gstr.Fields(str) ) fmt.Println(gstr.Equal(`A`Printf(`%#v`, `B`)result) // Output: // true // true // false }
切分组合
Split
[]string{"Hello", "World"} }
转义处理
AddSlashes
说明:
AddSlashes
将字符串中的符号前添加转义字符'\'
说明:Split
用delimiter
将str
拆分为[]string
。格式:
Code Block language go SplitAddSlashes(str, delimiter string) []string
示例:
Code Block language go func ExampleSplitExampleAddSlashes() { var ( str = `a|b|c|d` delimiter = `|``'aa'"bb"cc\r\n\d\t` result = gstr.SplitAddSlashes(str, delimiter) ) fmt.PrintfPrintln(`%#v`, result) // Output: // []string{"a", "b", "c", "d"} }
SplitAndTrim
// \'aa\'\"bb\"cc\\r\\n\\d\\t }
StripSlashes
说明:
StripSlashes
去掉字符串str
中的转义字符'\'
。说明:SplitAndTrim
使用delimiter
将str
拆分为[]string
,并对[]string
的每个元素调用Trim
,并忽略在Trim
之后为空的元素。格式:
Code Block language go SplitAndTrimStripSlashes(str, delimiter string, characterMask ...string) []string
示例:
Code Block language go func ExampleSplitAndTrimExampleStripSlashes() { var ( str = `a|b|||||c|d` delimiter = `|` result `C:\\windows\\GoFrame\\test` result = gstr.SplitAndTrimStripSlashes(str, delimiter) ) fmt.PrintfPrintln(`%#v`, result) // Output: // []string{"a", "b", "c", "d"}C:\windows\GoFrame\test }
Join
QuoteMeta
说明:
QuoteMeta
为str中'. \ + * ? [ ^ ] ( $ )
中的每个字符前添加一个转义字符'\'。
说明:Join
将array
中的每一个元素连接并生成一个新的字符串。参数sep
会作为新字符串的分隔符。格式:
Code Block language go JoinQuoteMeta(arraystr []string, sepchars ...string) string
示例:
Code Block language go func ExampleJoin() { var ( array = []string{"goframe", "is", "very", "easy", "to", "use"} sep = ` ` func ExampleQuoteMeta() { { var ( str = `.\+?[^]()` result = gstr.QuoteMeta(str) ) fmt.Println(result) } { var ( str = `https://goframe.org/pages/viewpage.action?pageId=1114327` result = gstr.Join(array, sepQuoteMeta(str) ) fmt.Println(result) .Println(result) } // Output: // \.\\\+\?\[\^\]\(\) // Outputhttps: // goframe is very easy to use }
JoinAny
goframe\.org/pages/viewpage\.action\?pageId=1114327 }
统计计数
Count
说明:
Count
计算substr
在s
中出现的次数。 如果在s
中没有找到substr
,则返回0
。 说明:JoinAny
将array
中的每一个元素连接并生成一个新的字符串。参数sep
会作为新字符串的分隔符。参数array
可以是任意的类型。格式:
Code Block language go JoinAny(array interface{}Count(s, sepsubstr string) stringint
示例:
Code Block language go func ExampleJoinAny() { var ( sep = `,` arr2 = []int{99, 73, 85, 66} resultExampleCount() { var ( str = `goframe is very, very easy to use` substr1 = "goframe" substr2 = "very" result1 = gstr.Count(str, substr1) result2 = gstr.JoinAnyCount(arr2str, sepsubstr2) ) fmt.Println(resultresult1) fmt.Println(result2) // Output: // 99,73,85,661 // 2 }
Explode
CountI
说明:
Count
计算substr
在s
中出现的次数,不区分大小写。 如果在s
中没有找到substr
,则返回0
。 说明:Explode
使用分隔符delimiter
字符串str
拆分成[]string
格式:
Code Block language go ExplodeCountI(delimiters, strsubstr string) []stringint
示例:
Code Block language go func ExampleExplodeExampleCountI() { var ( strstr = `goframe is very, very =easy `Helloto World`use` delimitersubstr1 = "GOFRAME" substr2 = "VERY" resultresult1 = gstr.CountI(str, substr1) result2 = gstr.ExplodeCountI(delimiterstr, strsubstr2) ) fmt.Printf(`%#v`, resultPrintln(result1) fmt.Println(result2) // Output: // []string{"Hello", "World"}1 // 2 }
Implode
CountWords
说明:
CountWords
以map[string]int
的形式返回str
中使用的单词的统计信息。说明:Implode
使用glue
连接pieces
字符串数组的每一个元素。格式:
Code Block language go ImplodeCountWords(gluestr string,) pieces map[string]string) stringint
示例:
Code Block language go func ExampleImplodeExampleCountWords() { var ( piecesstr = []string{"goframe", "is"`goframe is very, "very", "easy", "to", "use"} glue = " "use!` result = gstr.Implode(glue, piecesCountWords(str) ) fmt.PrintlnPrintf(`%#v`, result) // Output: // map[string]int{"easy":1, "goframe":1, "is very easy to use }
ChunkSplit
":1, "to":1, "use!":1, "very":1, "very,":1} }
CountChars
说明:
CountChars
以map[string]int
的形式返回str
中使用的字符的统计信息。noSpace
参数可以控制是否计算空格。说明:ChunkSplit
将字符串拆分为单位为chunkLen
长度更小的每一份,并用end
连接每一份拆分出的字符串。格式:
Code Block language go ChunkSplitCountChars(bodystr string, chunkLen int, end stringnoSpace ...bool) map[string]int
示例:
Code Block language go func ExampleChunkSplitExampleCountChars() { var ( body str = `1234567890` chunkLen = 2 end = "#"`goframe` result = gstr.ChunkSplit(body, chunkLen, endCountChars(str) ) fmt.Println(result) // May Output: // 12#34#56#78#90# }
转义处理
AddSlashes
map[a:1 e:1 f:1 g:1 m:1 o:1 r:1] }
数组处理
SearchArray
说明:
SearchArray
在[]string 'a'
中区分大小写地搜索字符串's'
,返回其在'a'
中的索引。 如果在'a'
中没有找到's'
,则返回-1
。 说明:AddSlashes
将字符串中的符号前添加转义字符'\'
格式:
Code Block language go AddSlashes(strSearchArray(a []string, s string) stringint
示例:
Code Block language go func ExampleAddSlashesExampleSearchArray() { var ( array = []string{"goframe", "is", "very", "nice"} str = `'aa'"bb"cc\r\n\d\t``goframe` result = gstr.AddSlashesSearchArray(array, str) ) fmt.Println(result) // Output: // \'aa\'\"bb\"cc\\r\\n\\d\\t }
StripSlashes
// Output: // 0 }
InArray
说明:
InArray校验
[]string 'a'
中是否有字符串' s '
。 说明:StripSlashes
去掉字符串str
中的转义字符'\'
。格式:
Code Block language go StripSlashes(strInArray(a []string, s string) stringbool
示例:
Code Block language go func ExampleStripSlashesExampleInArray() { var ( stra = []string{"goframe", "is", "very", "easy", "to", "use"} s = `C:\\windows\\GoFrame\\test`"goframe" result = gstr.StripSlashes(strInArray(a, s) ) fmt.Println(result) // Output: // C:\windows\GoFrame\testtrue }
QuoteMeta
PrefixArray
说明:
QuoteMeta
为str中'. \ + * ? [ ^ ] ( $ )
中的每个字符前添加一个转义字符'\'。
PrefixArray
位[]string array
的每一个字符串添加'prefix'
的前缀。格式:
Code Block language go QuoteMetaPrefixArray(strarray []string, charsprefix ...string) string
示例:
Code Block language go func ExampleQuoteMeta() { { var ( str = `.\+?[^]()` result = gstr.QuoteMeta(str) ) fmt.Println(result) } { ExamplePrefixArray() { var ( strstrArray = []string{"tom", = `https://goframe.org/pages/viewpage.action?pageId=1114327` result = gstr.QuoteMeta(str) ) "lily", "john"} ) gstr.PrefixArray(strArray, "classA_") fmt.Println(resultstrArray) } // Output: // \.\\\+\?\[\^\]\(\) // https://goframe\.org/pages/viewpage\.action\?pageId=1114327 }
统计计数
Count
[classA_tom classA_lily classA_john] }
命名转换
CaseCamel
说明:
CaseCamel
将字符串转换为大驼峰形式(首字母大写)。说明:Count
计算substr
在s
中出现的次数。 如果在s
中没有找到substr
,则返回0
。格式:
Code Block language go CountCaseCamel(s, substr string) intstring
示例:
Code Block language go func ExampleCountExampleCaseCamel() { var ( str = `goframe is very, very easy to use``hello world` substr1result = "goframe" substr2 = "very" result1 = gstr.CountCaseCamel(str, substr1) result2 = gstr.Count(str, substr2) ) fmt.Println(result1result) fmt.Println(result2) // Output: // 1 // 2HelloWorld }
CountI
CaseCamelLower
说明:
CaseCamelLower
将字符串转换为小驼峰形式(首字母小写)。说明:Count
计算substr
在s
中出现的次数,不区分大小写。 如果在s
中没有找到substr
,则返回0
。格式:
Code Block language go CountICaseCamelLower(s, substr string) intstring
示例:
Code Block language go func ExampleCountIExampleCaseCamelLower() { var ( str = `goframe is very, very easy to use``hello world` substr1result = "GOFRAME" substr2 = "VERY" result1 = gstr.CountI(str, substr1) result2 = gstr.CountI(str, substr2gstr.CaseCamelLower(str) ) fmt.Println(result1result) fmt.Println(result2) // Output: // 1 // 2 helloWorld }
CountWords
CaseSnake
说明:
CaseSnake
将字符串转换中的符号(下划线,空格,点,中横线)用下划线(_
)替换,并全部转换为小写字母。说明:CountWords
以map[string]int
的形式返回str
中使用的单词的统计信息。格式:
Code Block language go CountWordsCaseSnake(strs string) map[string]int
示例:
Code Block language go func ExampleCountWordsExampleCaseSnake() { var ( str = `goframe is very, very easy to use!``hello world` result = gstr.CountWordsCaseSnake(str) ) fmt.PrintfPrintln(`%#v`, result) // Output: // map[string]int{"easy":1, "goframe":1, "is":1, "to":1, "use!":1, "very":1, "very,":1}hello_world }
CountChars
CaseSnakeScreaming
说明:
CaseSnakeScreaming
把字符串中的符号(下划线,空格,点,中横线),全部替换为下划线'_'
,并将所有英文字母转为大写。说明:CountChars
以map[string]int
的形式返回str
中使用的字符的统计信息。noSpace
参数可以控制是否计算空格。格式:
Code Block language go CountCharsCaseSnakeScreaming(strs string, noSpace ...bool) map[string]int
示例:
Code Block language go func ExampleCountCharsExampleCaseSnakeScreaming() { var ( str = `hello `goframe`world` result = gstr.CountCharsCaseSnakeScreaming(str) ) fmt.Println(result) // May Output: // map[a:1 e:1 f:1 g:1 m:1 o:1 r:1]HELLO_WORLD }
CaseSnakeFirstUpper
SearchArray
说明:
SearchArray
在[]string 'a'
中区分大小写地搜索字符串's'
,返回其在'a'
中的索引。 如果在'a'
中没有找到's'
,则返回-1
。 说明:CaseSnakeFirstUpper
将字符串中的字母为大写时,将大写字母转换为小写字母并在其前面增加一个下划线'_'
,首字母大写时,只转换为小写,前面不增加下划线'_'
。格式:
Code Block language go SearchArrayCaseSnakeFirstUpper(aword []string, sunderscore ...string) intstring
示例:
Code Block language go func ExampleSearchArrayExampleCaseSnakeFirstUpper() { var ( array = []string{"goframe", "is", "very", "nice"}str = `RGBCodeMd5` result = gstr.CaseSnakeFirstUpper(str) ) fmt.Println(result) // Output: // rgb_code_md5 }
CaseKebab
说明:
CaseKebab
将字符串转换中的符号(下划线,空格,点,)用中横线'-'
替换,并全部转换为小写字母。格式:
Code Block language go CaseKebab(s string) string
示例:
Code Block language go func ExampleCaseKebab() { var ( str = `hello `goframe`world` result = gstr.SearchArrayCaseKebab(array, str) ) fmt.Println(result) // Output: // 0hello-world }
InArray
CaseKebabScreaming
说明:
CaseKebabScreaming
将字符串转换中的符号(下划线,空格,点,中横线)用中横线'-'
替换,并全部转换为大写字母。说明:InArray校验
[]string 'a'
中是否有字符串' s '
。格式:
Code Block language go InArray(a []string, CaseKebabScreaming(s string) boolstring
示例:
Code Block language go func ExampleInArrayExampleCaseKebabScreaming() { var ( a = []string{"goframe", "is", "very", "easy", "to", "use"} ( sstr = `hello = "goframe"world` result = gstr.InArray(a, sCaseKebabScreaming(str) ) fmt.Println(result) // Output: // trueHELLO-WORLD }
PrefixArray
CaseDelimited
说明:
PrefixArray
位[]string array
的每一个字符串添加'prefix'
的前缀。CaseDelimited
将字符串转换中的符号进行替换。格式:
Code Block language go PrefixArrayCaseDelimited(arrays []string, del prefixbyte) string)
示例:
Code Block language go func ExamplePrefixArrayExampleCaseDelimited() { var ( strArraystr = []string{"tom", "lily", "john"} ) gstr.PrefixArray(strArray, "classA_") `hello world` del = byte('-') result = gstr.CaseDelimited(str, del) ) fmt.Println(strArrayresult) // Output: // [classA_tom classA_lily classA_john] // hello-world }
命名转换
CaseCamel
CaseDelimitedScreaming
说明:
CaseCamel
将字符串转换为大驼峰形式(首字母大写)。CaseDelimitedScreaming
将字符串中的符号(空格,下划线,点,中横线)用第二个参数进行替换,该函数第二个参数为替换的字符,第三个参数为大小写转换,true
为全部转换大写字母,false
为全部转为小写字母。格式:
Code Block language go CaseCamelCaseDelimitedScreaming(s string, del uint8, screaming bool) string
示例:
Code Block language go language go func ExampleCaseDelimitedScreaming() { { var ( str = `hello world` del = byte('-') result = gstr.CaseDelimitedScreaming(str, del, true) ) fmt.Println(result) } { func ExampleCaseCamel() { var ( str = `hello world` del = byte('-') result = gstr.CaseCamelCaseDelimitedScreaming(str, del, false) ) fmt.Println(result) } // Output: // HelloWorldHELLO-WORLD // hello-world }
包含判断
CaseCamelLower
Contains
说明:
CaseCamelLower
将字符串转换为小驼峰形式(首字母小写)。Contains
返回字符串str
是否包含子字符串substr
,区分大小写。格式:
Code Block language go CaseCamelLower(sContains(str, substr string) stringbool
示例:
Code Block language go func ExampleCaseCamelLowerExampleContains() { { var ( str = `Hello World` str substr = `hello world``Hello` result = gstr.CaseCamelLowerContains(str, substr) ) fmt.Println(result) // Output: // helloWorld }
CaseSnake
说明:
CaseSnake
将字符串转换中的符号(下划线,空格,点,中横线)用下划线(_
)替换,并全部转换为小写字母。格式:
Code Block language go CaseSnake(s string) string
示例:
Code Block language go func ExampleCaseSnake() { } { var ( str = `hello world` `Hello World` substr = `hello` result = gstr.CaseSnakeContains(str, substr) ) fmt.Println(result) } // Output: // true // hello_worldfalse }
CaseSnakeScreaming
ContainsI
说明:
ContainsI
校验substr
是否在str
中,不区分大小写。 说明:CaseSnakeScreaming
把字符串中的符号(下划线,空格,点,中横线),全部替换为下划线'_'
,并将所有英文字母转为大写。格式:
Code Block language go CaseSnakeScreaming(sContainsI(str, substr string) stringbool
示例:
Code Block language go func ExampleCaseSnakeScreamingExampleContainsI() { var ( str = `Hello World` substr = `hello world`"hello" result1 = gstr.Contains(str, substr) resultresult2 = gstr.CaseSnakeScreamingContainsI(str, substr) ) fmt.Println(resultresult1) fmt.Println(result2) // Output: // HELLO_WORLDfalse // true }
CaseSnakeFirstUpper
ContainsAny
说明:
ContainsAny
校验s
中是否包含chars
说明:CaseSnakeFirstUpper
将字符串中的字母为大写时,将大写字母转换为小写字母并在其前面增加一个下划线'_'
,首字母大写时,只转换为小写,前面不增加下划线'_'
。格式:
Code Block language go CaseSnakeFirstUpperContainsAny(word strings, underscorechars ...string) stringbool
示例:
Code Block language go func ExampleCaseSnakeFirstUpperExampleContainsAny() { { var ( str s = `goframe` chars = `RGBCodeMd5`"g" result = gstr.CaseSnakeFirstUpper(strContainsAny(s, chars) ) fmt.Println(result) // Output: // rgb_code_md5 }
CaseKebab
说明:
CaseKebab
将字符串转换中的符号(下划线,空格,点,)用中横线'-'
替换,并全部转换为小写字母。格式:
Code Block language go CaseKebab(s string) string
示例:
Code Block language go func ExampleCaseKebab() { fmt.Println(result) } { var ( strs = `hello world` `goframe` chars = "G" result = gstr.CaseKebab(strContainsAny(s, chars) ) fmt.Println(result) } // Output: // hello-worldtrue // false }
CaseKebabScreaming
字符串转换
Chr
说明:
Chr
返回一个数字0-255
对应的ascii
字符串。 说明:CaseKebabScreaming
将字符串转换中的符号(下划线,空格,点,中横线)用中横线'-'
替换,并全部转换为大写字母。格式:
Code Block language go CaseKebabScreamingChr(sascii stringint) string
示例:
Code Block language go func ExampleCaseKebabScreamingExampleChr() { var ( strascii = 65 = `hello world`// A result = gstr.CaseKebabScreamingChr(strascii) ) fmt.Println(result) // Output: // HELLO-WORLDA }
CaseDelimited
Ord
说明:
CaseDelimited
将字符串转换中的符号进行替换。Ord
将字符串的第一个字节转换为0-255
之间的值。格式:
Code Block language go CaseDelimitedOrd(schar string, del byte) stringint
示例:
Code Block language go func ExampleCaseDelimitedExampleOrd() { var ( str = `hello world` del = byte('-')`goframe` result = gstr.CaseDelimitedOrd(str, del) ) fmt.Println(result) // Output: // hello-world103 }
CaseDelimitedScreaming
OctStr
说明:
OctStr
将字符串str
中的八进制字符串转换为其原始字符串。说明:CaseDelimitedScreaming
将字符串中的符号(空格,下划线,点,中横线)用第二个参数进行替换,该函数第二个参数为替换的字符,第三个参数为大小写转换,true
为全部转换大写字母,false
为全部转为小写字母。格式:
Code Block language go CaseDelimitedScreamingOctStr(sstr string, del uint8, screaming bool) string
示例:
Code Block language go func ExampleCaseDelimitedScreaming() { { var ( str = `hello world` delfunc ExampleOctStr() { var ( str = byte('-')`\346\200\241` result = gstr.CaseDelimitedScreamingOctStr(str, del, true) ) fmt.Println(result) } {// Output: var ( str = // 怡 }
Reverse
说明:
Reverse
返回str
的反转字符串。格式:
Code Block language go Reverse(str string) string
示例:
Code Block language go func ExampleReverse() { var ( str`hello world` del = byte('-') `123456` result = gstr.CaseDelimitedScreaming(str, del, falseReverse(str) ) fmt.Println(result) } // Output: // HELLO-WORLD // hello-world654321 }
包含判断
Contains
NumberFormat
说明:
NumberFormat
以千位分组来格式化数字。- 参数
decimal
设置小数点的个数。 - 参数
decPoint
设置小数点的分隔符。 参数
thousand
设置千位分隔符。
Contains
返回字符串str
是否包含子字符串substr
,区分大小写。- 参数
格式:
Code Block language go Contains(strNumberFormat(number float64, decimals int, decPoint, substrthousandsSep string) boolstring
示例:
Code Block language go func ExampleContainsExampleNumberFormat() { { var ( str var ( number float64 = 123456 decimals = 2 decPoint = "." thousandsSep = `Hello World` substr = `Hello`"," result = gstr.Contains(str, substr) ) fmt.Println(result) } { var ( str = `Hello World` substr = `hello` result = gstr.Contains(strNumberFormat(number, decimals, decPoint, substrthousandsSep) ) fmt.Println(result) } // Output: // true // false123,456.00 }
ContainsI
Shuffle
说明:
ContainsI
校验substr
是否在str
中,不区分大小写。Shuffle
返回将str
随机打散后的字符串。格式:
Code Block language go ContainsIShuffle(str, substr string) boolstring
示例:
Code Block language go func ExampleContainsIExampleShuffle() { var ( str = `Hello World``123456` substr = "hello" result1result = gstr.Contains(str, substr) result2 = gstr.ContainsIShuffle(str, substr) ) fmt.Println(result1result) fmt.Println(result2) // May Output: // false // true 563214 }
ContainsAny
HideStr
说明:
ContainsAny
校验s
中是否包含chars
。HideStr
将字符串str
从中间字符开始,百分比percent
的字符转换成hide
字符串。格式:
Code Block language go ContainsAny(sHideStr(str string, percent int, charshide string) boolstring
示例:
Code Block language go func ExampleContainsAnyExampleHideStr() { { var ( sstr = `13800138000` percent = `goframe`40 charshide = "g" `*` result = gstr.ContainsAny(sHideStr(str, percent, charshide) ) 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 ( s = `goframe` chars = "G" str = `goframe is very easy to use` result = gstr.ContainsAny(s, charsNl2Br(str) ) fmt.Println(result) } // Output: // true // false goframe<br>is<br>very<br>easy<br>to<br>use }
WordWrap
Chr
说明:
Chr
返回一个数字0-255
对应的ascii
字符串。 说明:WordWrap
使用换行符将str
换行到给定字符数(不会切分单词)。格式:
Code Block language go Chr(ascii intWordWrap(str string, width int, br string) string
示例:
Code Block language go go func ExampleWordWrap() { { var ( str = `A very long woooooooooooooooooord. and something` width = 8 br = "\n" func ExampleChr() { var ( ascii = 65 // A result = gstr.Chr(asciiWordWrap(str, width, br) ) 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 var ( str = `The quick brown fox jumped over the lazy dog.` width = 20 br = `goframe` "<br />\n" result = gstr.OrdWordWrap(str, width, br) ) fmt.PrintlnPrintf("%v", 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: // 怡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: } // 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[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
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
字符串。 说明:Pos
返回needle
在haystack
中第一次出现的位置,区分大小写。 如果没有找到,则返回-1。格式:
Code Block language go PosPosRune(haystack, needle string, startOffset ...int) int
示例:
Code Block language go func ExamplePosExamplePosRune() { var ( haystack = `Hello World` `GoFrame是一款模块化、高性能、企业级的Go基础开发框架` needle = `Go` needleposI = `World` gstr.PosRune(haystack, needle) resultposR = gstr.PosPosRRune(haystack, needle) ) fmt.Println(posI) fmt.Println(resultposR) // Output: // 60 // 22 }
PosRune
PosI
说明:
PosRune
的作用于函数Pos
相似,但支持haystack
和needle
为unicode
字符串。PosI
返回needle
在haystack
中第一次出现的位置,不区分大小写。 如果没有找到,则返回-1。格式:
Code Block language go PosRunePosI(haystack, needle string, startOffset ...int) int
示例:
Code Block language go func ExamplePosRuneExamplePosI() { var ( haystack = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架``goframe is very, very easy to use` needle = `Go``very` posI = gstr.PosRunePosI(haystack, needle) posR = gstr.PosRRunePosR(haystack, needle) ) fmt.Println(posI) fmt.Println(posR) // Output: // 011 // 2217 }
PosI
PosRuneI
说明:
PosRuneI
的作用于函数PosI
返回needle
在haystack
中第一次出现的位置,不区分大小写。 如果没有找到,则返回-1。 相似,但支持haystack
和needle
为unicode
字符串。格式:
Code Block language go PosIPosIRune(haystack, needle string, startOffset ...int) int
示例:
Code Block language go func ExamplePosIExamplePosIRune() { { var ( haystack = `goframe is very, very easy to use` needle = `very` posI`GoFrame是一款模块化、高性能、企业级的Go基础开发框架` needle = `高性能` startOffset = 10 result = gstr.PosIPosIRune(haystack, needle, startOffset) posR) fmt.Println(result) } { var ( haystack = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架` needle = `高性能` startOffset = 30 result = gstr.PosRPosIRune(haystack, needle, startOffset) ) fmt.Println(posIresult) fmt.Println(posR)} // Output: // 1114 // 17-1 }
PosRuneI
PosR
说明:
PosRuneI
的作用于函数PosI
相似,但支持haystack
和needle
为unicode
字符串。PosR
返回needle
在haystack
中最后一次出现的位置,区分大小写。 如果没有找到,则返回-1。格式:
Code Block language go PosIRunePosR(haystack, needle string, startOffset ...int) int
示例:
Code Block language go func ExamplePosIRuneExamplePosR() { { var ( haystack = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架` needle = `高性能` startOffset = 10 result = gstr.PosIRune(haystack, needle, startOffset) ) fmt.Println(result) } { var ( haystack = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架` needle`goframe is very, very easy to use` needle = `very` posI = `高性能` startOffset = 30gstr.PosI(haystack, needle) result posR = gstr.PosIRunePosR(haystack, needle, startOffset) ) fmt.Println(resultposI) }fmt.Println(posR) // Output: // 1411 // -117 }
PosR
PosRuneR
说明:
PosRuneR
的作用于函数PosR
返回needle
在haystack
中最后一次出现的位置,区分大小写。 如果没有找到,则返回-1。 相似,但支持haystack
和needle
为unicode
字符串。格式:
Code Block language go PosRPosRRune(haystack, needle string, startOffset ...int) int
示例:
Code Block language go func ExamplePosRExamplePosRRune() { var ( haystack = `goframe is very, very easy to use``GoFrame是一款模块化、高性能、企业级的Go基础开发框架` needle = `very``Go` posI = gstr.PosIPosIRune(haystack, needle) posR = gstr.PosRPosRRune(haystack, needle) ) fmt.Println(posI) fmt.Println(posR) // Output: // 110 // 1722 }
PosRuneR
PosRI
说明:
PosRuneR
的作用于函数PosR
相似,但支持haystack
和needle
为unicode
字符串。PosRI
返回needle
在haystack
中最后一次出现的位置,不区分大小写。 如果没有找到,则返回-1。格式:
Code Block language go PosRRunePosRI(haystack, needle string, startOffset ...int) int
示例:
Code Block language go func ExamplePosRRuneExamplePosRI() { var ( haystack = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架` `goframe is very, very easy to use` needle = `Go``VERY` posI = gstr.PosIRunePosI(haystack, needle) posR = gstr.PosRRunePosRI(haystack, needle) ) fmt.Println(posI) fmt.Println(posR) // Output: // 011 // 2217 }
PosRI
PosRIRune
说明:
PosRIRune
的作用于函数PosRI
返回needle
在haystack
中最后一次出现的位置,不区分大小写。 如果没有找到,则返回-1。 相似,但支持haystack
和needle
为unicode
字符串。格式:
Code Block language go PosRIPosRIRune(haystack, needle string, startOffset ...int) int
示例:
Code Block language go func ExamplePosRIExamplePosRIRune() { var ( haystack = `goframe is very, very easy to use``GoFrame是一款模块化、高性能、企业级的Go基础开发框架` needle = `VERY``GO` posI = gstr.PosIPosIRune(haystack, needle) posR = gstr.PosRIPosRIRune(haystack, needle) ) fmt.Println(posI) fmt.Println(posR) // Output: // 110 // 1722 }
查找替换
PosRIRune
Replace
说明:
Replace
返回origin
字符串中,search
被replace
替换后的新字符串。search
区分大小写。说明:PosRIRune
的作用于函数PosRI
相似,但支持haystack
和needle
为unicode
字符串。格式:
Code Block language go PosRIRune(haystackReplace(origin, search, needlereplace string, startOffsetcount ...int) intstring
示例:
Code Block language go func ExamplePosRIRuneExampleReplace() { var ( haystackorigin = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架` `golang is very nice!` needle search = `GO``golang` posI replace = gstr.PosIRune(haystack, needle)`goframe` posR result = gstr.PosRIRune(haystackReplace(origin, search, needlereplace) ) fmt.Println(posIresult) fmt.Println(posR) // Output: // 0 // 22goframe is very nice! }
ReplaceI
Replace
说明:
Replace
ReplaceI
返回origin
字符串中,search
被replace
替换后的新字符串。search
区分大小写。不区分大小写。格式:
Code Block language go ReplaceReplaceI(origin, search, replace string, count ...int) string
示例:
Code Block language go func ExampleReplaceExampleReplaceI() { var ( origin = `golang is very nice!` search = `golang``GOLANG` replace = `goframe` result = gstr.ReplaceReplaceI(origin, search, replace) ) fmt.Println(result) // Output: // goframe is very nice! }
ReplaceI
ReplaceByArray
说明:
ReplaceI
ReplaceByArray
返回origin
字符串中被一个切片按两个一组(search,
search
被replace
替换后的新字符串。search
不区分大小写。replace)
顺序替换的新字符串,区分大小写。格式:
Code Block language go ReplaceIReplaceByArray(origin, searchstring, replacearray string, count ...int[]string) string
示例:
Code Block language go func ExampleReplaceIExampleReplaceByArray() { { var ( origin = `golang is very nice!`nice` search array = `GOLANG` []string{"lang", "frame"} replaceresult = `goframe`gstr.ReplaceByArray(origin, array) ) fmt.Println(result) } { var ( origin = `golang is very good` array = gstr.ReplaceI(origin, search, replace) ) []string{"golang", "goframe", "good", "nice"} result = gstr.ReplaceByArray(origin, array) ) fmt.Println(result) } // Output: // goframe is very nice // goframe is very nice! }
ReplaceByArray
ReplaceIByArray
说明:
ReplaceByArray
ReplaceIByArray
返回origin
被一个切片按两个一组(search, replace)
顺序替换的新字符串,区分大小写。 顺序替换的新字符串,不区分大小写。格式:
Code Block language go ReplaceByArrayReplaceIByArray(origin string, array []string) string
示例:
Code Block language go func ExampleReplaceByArrayExampleReplaceIByArray() { { var ( origin = `golang is very nice`Good` array = []string{"lang"Golang", "goframe", "GOOD", "framenice"} 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() { ReplaceByArray(origin, array) ) fmt.Println(result) } { var ( origin = `golang is very good`nice` array replaces = map[string]string{ "golanglang",: "goframe", "good", "nice"} result = gstr.ReplaceByArray(origin, array)frame", ) 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() { result = gstr.ReplaceByMap(origin, replaces) ) fmt.Println(result) } { var ( origin = `golang is very Good`good` array replaces = map[string]string{ "Golanggolang",: "goframe", "good": "GOODnice", "nice" } result = gstr.ReplaceIByArrayReplaceByMap(origin, arrayreplaces) ) fmt.Println(result) } // Output: // goframe is very nice // goframe is very nice }
ReplaceByMap
ReplaceIByMap
说明:
ReplaceByMap
ReplaceIByMap
返回origin
中map
的key
替换为value
的新字符串,区分大小写。 的新字符串,不区分大小写。格式:
Code Block language go ReplaceByMapReplaceIByMap(origin string, replaces map[string]string) string
示例:
Code Block language go func ExampleReplaceByMapExampleReplaceIByMap() { { var ( origin = `golang is very nice` replaces = map[string]string{ "langLang": "frame", } result = gstr.ReplaceByMapReplaceIByMap(origin, replaces) ) fmt.Println(result) } { var ( origin = `golang is very good` replaces = map[string]string{ "golang": "goframe", "good": "nice", } result = gstr.ReplaceByMap(origin, replaces) ) // 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: // goframe is very nice // goframe is very nice }
ReplaceIByMap
.jpg }
StrEx
说明:
StrEx
返回从needle
第一次出现的位置开始,到haystack
结尾的字符串(不包含needle
本身)。说明:ReplaceIByMap
返回origin
中map
的key
替换为value
的新字符串,不区分大小写。格式:
Code Block language go ReplaceIByMapStrEx(originhaystack string, replacesneedle map[string]string) string
示例:
Code Block language go func ExampleReplaceIByMapExampleStrEx() { var ( originhaystack = `golang is very nice` replaces = map[string]string{ "Lang": "frame", } result= `https://goframe.org/index.html?a=1&b=2` needle = `?` result = gstr.ReplaceIByMapStrEx(originhaystack, replacesneedle) ) fmt.Println(result) // Output: // goframe is very nicea=1&b=2 }
子串截取
Str
StrTill
说明:
Str
StrTill
返回从haystack
字符串开始到needle
第一次出现的位置开始,到haystack
结尾的字符串(包含第一次出现的位置的字符串(包含needle
本身)。格式:
Code Block language go StrStrTill(haystack string, needle string) string
示例:
Code Block language go func ExampleStrExampleStrTill() { var ( haystack = `xxx.jpg``https://goframe.org/index.html?test=123456` needle = `.?` result = gstr.StrStrTill(haystack, needle) ) fmt.Println(result) // Output: // .jpghttps://goframe.org/index.html? }
StrEx
StrTillEx
说明:
StrEx
StrTillEx
返回从haystack
字符串开始到needle
第一次出现的位置开始,到haystack
结尾的字符串(不包含第一次出现的位置的字符串(不包含needle
本身)。格式:
Code Block language go StrExStrTillEx(haystack string, needle string) string
示例:
Code Block language go func ExampleStrExExampleStrTillEx() { var ( haystack = `https://goframe.org/index.html?a=1&b=2`test=123456` needle = `?` result = gstr.StrExStrTillEx(haystack, needle) ) fmt.Println(result) // Output: // a=1&b=2https://goframe.org/index.html }
StrTill
SubStr
说明:
SubStr
返回字符串str
从start
开始,长度为length
的新字符串。 参数length
是可选的,它默认使用str
的长度。 说明:StrTill
返回从haystack
字符串开始到needle
第一次出现的位置的字符串(包含needle
本身)。格式:
Code Block language go StrTillSubStr(haystackstr string, needle string) stringstart int, length ...int) (substr string)
示例:
Code Block language go func ExampleStrTillExampleSubStr() { var ( ( str = `1234567890` haystackstart = `https://goframe.org/index.html?test=123456`0 needlelength = `?`4 result subStr = gstr.StrTill(haystackSubStr(str, start, needlelength) ) fmt.Println(resultsubStr) // Output: // https://goframe.org/index.html?1234 }
StrTillEx
SubStrRune
说明:
SubStrRune
返回unicode
字符串str
从start
开始,长度为length
的新字符串。 参数length
是可选的,它默认使用str
的长度。 说明:StrTillEx
返回从haystack
字符串开始到needle
第一次出现的位置的字符串(不包含needle
本身)。格式:
Code Block language go StrTillExSubStrRune(haystackstr string, needle string)start int, length ...int) (substr string)
示例:
Code Block language go func ExampleStrTillExExampleSubStrRune() { var ( haystack = `https://goframe.org/index.html?test=123456` str = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架。` needle start = `?`14 resultlength = 3 subStr = gstr.StrTillEx(haystackSubStrRune(str, start, needlelength) ) fmt.Println(resultsubStr) // Output: // https://goframe.org/index.html高性能 }
SubStr
StrLimit
说明:
SubStr
返回字符串str
从start
开始,长度为length
的新字符串。 参数length
是可选的,它默认使用str
的长度。StrLimit
取str
字符串开始,长度为length
的字符串,加上suffix...
后返回新的字符串。格式:
Code Block language go SubStrStrLimit(str string, startlength int, lengthsuffix ...intstring) (substr string)
示例:
Code Block language go func ExampleSubStrExampleStrLimit() { var ( str = `1234567890``123456789` start length = 03 lengthsuffix = 4`...` subStrresult = gstr.SubStrStrLimit(str, startlength, lengthsuffix) ) fmt.Println(subStrresult) // Output: // 1234123... }
SubStrRune
StrLimitRune
说明:
SubStrRune
返回StrLimitRune
取unicode
字符串str
从start
开始,长度为length
的新字符串。 参数length
是可选的,它默认使用str
的长度。 的字符串,加上suffix...
后返回新的字符串。格式:
Code Block language go SubStrRuneStrLimitRune(str string, startlength int, lengthsuffix ...intstring) (substr string)
示例:
Code Block language go func ExampleSubStrRuneExampleStrLimitRune() { var ( str = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架。` start length = 1417 lengthsuffix = 3"..." subStrresult = gstr.SubStrRuneStrLimitRune(str, startlength, lengthsuffix) ) fmt.Println(subStrresult) // Output: // 高性能GoFrame是一款模块化、高性能... }
StrLimit
SubStrFrom
说明:
StrLimit
取str
字符串开始,长度为length
的字符串,加上suffix...
后返回新的字符串。SubStrFrom
返回字符串str
从第一次出现need
到str
的结尾的字符串(包含need
)。格式:
Code Block language go StrLimitSubStrFrom(str string, lengthneed int, suffix ...string) (substr string) string
示例:
Code Block language go func ExampleStrLimitExampleSubStrFrom() { var ( str = `123456789` length = 3var ( suffixstr = `...`"我爱GoFrameGood" resultneed = gstr.StrLimit(str, length, suffix)`爱` ) fmt.Println(result(gstr.SubStrFrom(str, need)) // Output: // 123...爱GoFrameGood }
StrLimitRune
SubStrFromEx
说明:
StrLimitRune
取unicode
字符串str
开始,长度为length
的字符串,加上suffix...
后返回新的字符串。SubStrFromEx
返回字符串str
从第一次出现need
到str
的结尾的字符串(不包含need
)。格式:
Code Block language go StrLimitRuneSubStrFromEx(str string, lengthneed int, suffix ...string) (substr string) string
示例:
Code Block language go func ExampleStrLimitRuneExampleSubStrFromEx() { var ( str = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架。` length = 17 suffix = "..." result = gstr.StrLimitRune(str, length, suffix) )"我爱GoFrameGood" need = `爱` ) fmt.Println(result(gstr.SubStrFromEx(str, need)) // Output: // GoFrame是一款模块化、高性能...GoFrameGood }
SubStrFrom
SubStrFromR
说明:
SubStrFrom
SubStrFromR
返回字符串str
从第一次出现从最后一次出现need
到str
的结尾的字符串(包含need
)。格式:
Code Block language go SubStrFromSubStrFromR(str string, need string) (substr string)
示例:
Code Block language go func ExampleSubStrFromExampleSubStrFromR() { var ( str = "我爱GoFrameGood" need = `爱``Go` ) fmt.Println(gstr.SubStrFromSubStrFromR(str, need)) // Output: // 爱GoFrameGoodGood }
SubStrFromEx
SubStrFromREx
说明:
SubStrFromEx
SubStrFromREx
返回字符串str
从第一次出现从最后一次出现need
到str
的结尾的字符串(不包含need
)。格式:
Code Block language go SubStrFromExSubStrFromREx(str string, need string) (substr string)
示例:
Code Block language go func ExampleSubStrFromExExampleSubStrFromREx() { var ( str = "我爱GoFrameGood" need = `爱``Go` ) fmt.Println(gstr.SubStrFromExSubStrFromREx(str, need)) // Output: // GoFrameGoodod }
SubStrFromR
字符/子串过滤
Trim
说明:
Trim
从字符串的开头和结尾剪切空白(或其他字符)。 可选参数characterMask
指定额外剥离的字符。说明:SubStrFromR
返回字符串str
从最后一次出现need
到str
的结尾的字符串(包含need
)。格式:
Code Block language go SubStrFromRTrim(str string, needcharacterMask ...string) (substr string)
示例:
Code Block language go func ExampleSubStrFromRExampleTrim() { var ( var ( str = `*Hello World*` str characterMask = "我爱GoFrameGood*d" needresult = `Go`gstr.Trim(str, characterMask) ) fmt.Println(gstr.SubStrFromR(str, need))result) // Output: // Hello GoodWorl }
SubStrFromREx
TrimStr
说明:
TrimStr
从字符串的开头和结尾去掉所有cut
字符串(不会删除开头或结尾的空白)。 说明:SubStrFromREx
返回字符串str
从最后一次出现need
到str
的结尾的字符串(不包含need
)。格式:
Code Block language go SubStrFromRExTrimStr(str string, needcut string) (substr, count ...int) string)
示例:
Code Block language go func ExampleSubStrFromRExExampleTrimStr() { var ( str = `Hello "我爱GoFrameGoodWorld` cut = "World" need = `Go` count = -1 result = gstr.TrimStr(str, cut, count) ) fmt.Println(gstr.SubStrFromREx(str, need)result) // Output: // odHello }
字符过滤
Trim
TrimLeft
说明:
Trim
从字符串的开头和结尾剪切空白TrimLeft
将字符串开头的空格(或其他字符)。 可选参数characterMask
指定额外剥离的字符。删除。格式:
Code Block language go TrimTrimLeft(str string, characterMask ...string) string
示例:
Code Block language go func ExampleTrimExampleTrimLeft() { var ( str = `*Hello World*` characterMask = "*" result = gstr.TrimTrimLeft(str, characterMask) ) fmt.Println(result) // Output: // Hello World* }
TrimStr
TrimLeftStr
说明:
TrimStr
从字符串的开头和结尾去掉所有TrimLeftStr
从字符串的开头删除count
个cut
字符串(不会删除开头或结尾的空白)。 不会删除开头的空格)。格式:
Code Block language go TrimStrTrimLeftStr(str string, cut string, count ...int) string
示例:
Code Block language go func ExampleTrimStrExampleTrimLeftStr() { var ( str = `Hello World``**Hello World**` cut = "World*" count = -1 result = gstr.TrimStrTrimLeftStr(str, cut, count) ) fmt.Println(result) // Output: // *Hello World** }
TrimLeft
TrimRight
说明:
TrimLeft
将字符串开头的空格TrimRight
从字符串的末尾去掉空白(或其他字符)删除。 。格式:
Code Block language go TrimLeftTrimRight(str string, characterMask ...string) string
示例:
Code Block language go func ExampleTrimLeftExampleTrimRight() { var ( str = `**Hello World**` characterMask = "*def" // []byte{"*", "d", "e", "f"} result = gstr.TrimLeftTrimRight(str, characterMask) ) fmt.Println(result) // Output: // **Hello World*Worl }
TrimLeftStr
TrimRightStr
说明:
TrimLeftStr
从字符串的开头删除TrimRightStr
从字符串的尾部删除count
个cut
字符串(不会删除开头的空格)。 不会删除尾部的空格)。格式:
Code Block language go TrimLeftStrTrimRightStr(str string, cut string, count ...int) string
示例:
Code Block language go func ExampleTrimLeftStrExampleTrimRightStr() { var ( str = `**Hello`Hello World**!` cut = "*!" count = -1 result = gstr.TrimLeftStrTrimRightStr(str, cut, count) ) fmt.Println(result) // Output: // *Hello World** }
TrimRight
TrimAll
说明:
TrimRight
从字符串的末尾去掉空白TrimAll
删除字符串str
中的所有空格(或其他字符)以及characterMask
字符。格式:
Code Block language go TrimRightTrimAll(str string, characterMask ...string) string
示例:
Code Block language go func ExampleTrimRightExampleTrimAll() { var ( str = `**Hello World**` characterMask = "*def" // []byte{"*", "d", "e", "f"} result = gstr.TrimRightTrimAll(str, characterMask) ) fmt.Println(result) // Output: // **Hello WorlHelloWorld }
TrimRightStr
HasPrefix
说明:
TrimRightStr
从字符串的尾部删除count
个cut
字符串(不会删除尾部的空格)。HasPrefix
返回s
是否以prefix
开头。格式:
Code Block language go TrimRightStrHasPrefix(str strings, cut string, count ...intprefix string) stringbool
示例:
Code Block language go func ExampleTrimRightStrExampleHasPrefix() { var ( strs = `Hello World!`World` cut prefix = "!" count = -1Hello" result = gstr.TrimRightStrHasPrefix(strs, cut, countprefix) ) fmt.Println(result) // Output: // Hello Worldtrue }
TrimAll
HasSuffix
说明:
HasSuffix
返回s
是否以suffix
结束。说明:TrimAll
删除字符串str
中的所有空格(或其他字符)以及characterMask
字符。格式:
Code Block language go TrimAllHasSuffix(str strings, characterMasksuffix ...string) stringbool
示例:
Code Block language go func ExampleTrimAllExampleHasSuffix() { var ( strs = `my best love = `*Hello World*`is goframe` characterMaskprefix = "*goframe" result = gstr.TrimAllHasSuffix(strs, characterMaskprefix) ) fmt.Println(result) // Output: // HelloWorldtrue }
版本比较
CompareVersion
说明:
CompareVersion
将a
和b
作为标准GNU
版本进行比较。格式:
Code Block language go CompareVersion(a, b string) int
示例:
Code Block language go func ExampleCompareVersion() { fmt.Println(gstr.CompareVersion("v2.11.9", "v2.10.8")) fmt.Println(gstr.CompareVersion("1.10.8", "1.19.7")) fmt.Println(gstr.CompareVersion("2.8.beta", "2.8")) // Output: // 1 // -1 // 0 }
CompareVersionGo
说明:
CompareVersionGo
将a
和b
作为标准的Golang
版本进行比较。格式:
Code Block language go CompareVersionGo(a, b string) int
示例:
Code Block language go func ExampleCompareVersionGo() { fmt.Println(gstr.CompareVersionGo("v2.11.9", "v2.10.8")) fmt.Println(gstr.CompareVersionGo("v4.20.1", "v4.20.1+incompatible")) fmt.Println(gstr.CompareVersionGo( "v0.0.2-20180626092158-b2ccc119800e", "v1.0.1-20190626092158-b2ccc519800e", )) // Output: // 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) ) fmt.Println(result) // Output: // 2 }
SimilarText
说明:
SimilarText
计算两个字符串之间的相似度。格式:
Code Block language go SimilarText(first, second string, percent *float64) int
示例:
Code Block language go func ExampleSimilarText() { var ( first = `AaBbCcDd` second = `ad` percent = 0.80 result = gstr.SimilarText(first, second, &percent) ) fmt.Println(result) // Output: // 2 }
Soundex
说明:
Soundex
用于计算字符串的Soundex
键。格式:
Code Block language go Soundex(str string) string
示例:
Code Block language go func ExampleSoundex() { var ( str1 = `Hello` str2 = `Hallo` result1 = gstr.Soundex(str1) result2 = gstr.Soundex(str2) ) fmt.Println(result1, result2) // Output: // H400 H400 }
Panel | ||
---|---|---|
| ||
|