gf/text/gstr/gstr_trim.go

115 lines
3.1 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
2019-02-01 14:38:45 +08:00
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
2019-02-01 14:38:45 +08:00
package gstr
2019-10-21 19:13:25 +08:00
import (
"strings"
2021-11-16 21:37:59 +08:00
"github.com/gogf/gf/v2/internal/utils"
2019-10-21 19:13:25 +08:00
)
2019-04-06 21:31:01 +08:00
// Trim strips whitespace (or other characters) from the beginning and end of a string.
// The optional parameter `characterMask` specifies the additional stripped characters.
2019-02-01 14:38:45 +08:00
func Trim(str string, characterMask ...string) string {
return utils.Trim(str, characterMask...)
2019-02-01 14:38:45 +08:00
}
// TrimStr strips all the given `cut` string from the beginning and end of a string.
2021-08-30 14:29:04 +08:00
// Note that it does not strip the whitespaces of its beginning or end.
func TrimStr(str string, cut string, count ...int) string {
return TrimLeftStr(TrimRightStr(str, cut, count...), cut, count...)
2019-10-21 19:13:25 +08:00
}
2019-04-06 21:31:01 +08:00
// TrimLeft strips whitespace (or other characters) from the beginning of a string.
2019-02-01 14:38:45 +08:00
func TrimLeft(str string, characterMask ...string) string {
trimChars := utils.DefaultTrimChars
2021-02-08 17:57:21 +08:00
if len(characterMask) > 0 {
trimChars += characterMask[0]
2019-06-19 09:06:52 +08:00
}
2021-02-08 17:57:21 +08:00
return strings.TrimLeft(str, trimChars)
2019-02-01 14:38:45 +08:00
}
// TrimLeftStr strips all the given `cut` string from the beginning of a string.
2021-08-30 14:29:04 +08:00
// Note that it does not strip the whitespaces of its beginning.
func TrimLeftStr(str string, cut string, count ...int) string {
var (
lenCut = len(cut)
cutCount = 0
)
for len(str) >= lenCut && str[0:lenCut] == cut {
str = str[lenCut:]
cutCount++
if len(count) > 0 && count[0] != -1 && cutCount >= count[0] {
break
}
2019-06-19 09:06:52 +08:00
}
return str
2019-02-01 14:38:45 +08:00
}
2019-04-06 21:31:01 +08:00
// TrimRight strips whitespace (or other characters) from the end of a string.
2019-02-01 14:38:45 +08:00
func TrimRight(str string, characterMask ...string) string {
trimChars := utils.DefaultTrimChars
2021-02-08 17:57:21 +08:00
if len(characterMask) > 0 {
trimChars += characterMask[0]
2019-06-19 09:06:52 +08:00
}
2021-02-08 17:57:21 +08:00
return strings.TrimRight(str, trimChars)
2019-02-01 14:38:45 +08:00
}
// TrimRightStr strips all the given `cut` string from the end of a string.
2021-08-30 14:29:04 +08:00
// Note that it does not strip the whitespaces of its end.
func TrimRightStr(str string, cut string, count ...int) string {
var (
lenStr = len(str)
lenCut = len(cut)
cutCount = 0
)
for lenStr >= lenCut && str[lenStr-lenCut:lenStr] == cut {
lenStr = lenStr - lenCut
str = str[:lenStr]
cutCount++
if len(count) > 0 && count[0] != -1 && cutCount >= count[0] {
break
}
2019-06-19 09:06:52 +08:00
}
return str
2019-02-01 14:38:45 +08:00
}
2021-02-08 17:57:21 +08:00
// TrimAll trims all characters in string `str`.
func TrimAll(str string, characterMask ...string) string {
trimChars := utils.DefaultTrimChars
2021-02-08 17:57:21 +08:00
if len(characterMask) > 0 {
trimChars += characterMask[0]
}
var (
filtered bool
slice = make([]rune, 0, len(str))
)
for _, char := range str {
filtered = false
for _, trimChar := range trimChars {
if char == trimChar {
filtered = true
break
}
}
if !filtered {
slice = append(slice, char)
}
}
return string(slice)
}
2021-12-21 23:24:38 +08:00
// HasPrefix tests whether the string s begins with prefix.
func HasPrefix(s, prefix string) bool {
return strings.HasPrefix(s, prefix)
}
// HasSuffix tests whether the string s ends with suffix.
func HasSuffix(s, suffix string) bool {
return strings.HasSuffix(s, suffix)
}