Files
gf/text/gstr/gstr_trim.go

84 lines
2.5 KiB
Go
Raw Normal View History

// Copyright 2019 gf Author(https://github.com/gogf/gf). 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"
)
var (
// defaultTrimChars are the characters which are stripped by Trim* functions in default.
defaultTrimChars = string([]byte{
'\t', // Tab.
'\v', // Vertical tab.
'\n', // New line (line feed).
'\r', // Carriage return.
'\f', // New page.
' ', // Ordinary space.
0x00, // NUL-byte.
0x85, // Delete.
0xA0, // Non-breaking space.
})
)
2019-02-01 14:38:45 +08:00
2019-04-06 21:31:01 +08:00
// Trim strips whitespace (or other characters) from the beginning and end of a string.
2019-10-21 19:13:25 +08:00
// The optional parameter <characterMask> specifies the additional stripped characters.
2019-02-01 14:38:45 +08:00
func Trim(str string, characterMask ...string) string {
2019-10-21 19:13:25 +08:00
if len(characterMask) == 0 {
return strings.Trim(str, defaultTrimChars)
2019-06-19 09:06:52 +08:00
} else {
2019-10-21 19:13:25 +08:00
return strings.Trim(str, defaultTrimChars+characterMask[0])
2019-06-19 09:06:52 +08:00
}
2019-02-01 14:38:45 +08:00
}
2019-10-21 19:13:25 +08:00
// TrimStr strips all of the given <cut> string from the beginning and end of a string.
// Note that it does not strips the whitespaces of its beginning or end.
func TrimStr(str string, cut string) string {
return TrimLeftStr(TrimRightStr(str, cut), cut)
}
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 {
2019-06-19 09:06:52 +08:00
if len(characterMask) == 0 {
2019-10-21 19:13:25 +08:00
return strings.TrimLeft(str, defaultTrimChars)
2019-06-19 09:06:52 +08:00
} else {
2019-10-21 19:13:25 +08:00
return strings.TrimLeft(str, defaultTrimChars+characterMask[0])
2019-06-19 09:06:52 +08:00
}
2019-02-01 14:38:45 +08:00
}
2019-04-06 21:31:01 +08:00
// TrimLeftStr strips all of the given <cut> string from the beginning of a string.
2019-10-21 19:13:25 +08:00
// Note that it does not strips the whitespaces of its beginning.
2019-02-01 14:38:45 +08:00
func TrimLeftStr(str string, cut string) string {
var lenCut = len(cut)
for len(str) >= lenCut && str[0:lenCut] == cut {
str = str[lenCut:]
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 {
2019-06-19 09:06:52 +08:00
if len(characterMask) == 0 {
2019-10-21 19:13:25 +08:00
return strings.TrimRight(str, defaultTrimChars)
2019-06-19 09:06:52 +08:00
} else {
2019-10-21 19:13:25 +08:00
return strings.TrimRight(str, defaultTrimChars+characterMask[0])
2019-06-19 09:06:52 +08:00
}
2019-02-01 14:38:45 +08:00
}
2019-04-06 21:31:01 +08:00
// TrimRightStr strips all of the given <cut> string from the end of a string.
2019-10-21 19:13:25 +08:00
// Note that it does not strips the whitespaces of its end.
2019-02-01 14:38:45 +08:00
func TrimRightStr(str string, cut string) string {
var lenStr = len(str)
var lenCut = len(cut)
for lenStr >= lenCut && str[lenStr-lenCut:lenStr] == cut {
lenStr = lenStr - lenCut
str = str[:lenStr]
2019-06-19 09:06:52 +08:00
}
return str
2019-02-01 14:38:45 +08:00
}