mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
strings, bytes: add CutPrefix and CutSuffix
Fixes #42537 Change-Id: Ie03c2614ffee30ebe707acad6b9f6c28fb134a45 Reviewed-on: https://go-review.googlesource.com/c/go/+/407176 Reviewed-by: Benny Siegert <bsiegert@gmail.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Changkun Ou <mail@changkun.de> Reviewed-by: Ian Lance Taylor <iant@google.com>
This commit is contained in:
parent
0df7ad2e79
commit
68005592b3
5 changed files with 136 additions and 0 deletions
|
|
@ -1187,3 +1187,25 @@ func Cut(s, sep string) (before, after string, found bool) {
|
|||
}
|
||||
return s, "", false
|
||||
}
|
||||
|
||||
// CutPrefix returns s without the provided leading prefix string
|
||||
// and reports whether it found the prefix.
|
||||
// If s doesn't start with prefix, CutPrefix returns s, false.
|
||||
// If prefix is the empty string, CutPrefix returns s, true.
|
||||
func CutPrefix(s, prefix string) (after string, found bool) {
|
||||
if !HasPrefix(s, prefix) {
|
||||
return s, false
|
||||
}
|
||||
return s[len(prefix):], true
|
||||
}
|
||||
|
||||
// CutSuffix returns s without the provided ending suffix string
|
||||
// and reports whether it found the suffix.
|
||||
// If s doesn't end with suffix, CutSuffix returns s, false.
|
||||
// If suffix is the empty string, CutSuffix returns s, true.
|
||||
func CutSuffix(s, suffix string) (after string, found bool) {
|
||||
if !HasSuffix(s, suffix) {
|
||||
return s, false
|
||||
}
|
||||
return s[:len(s)-len(suffix)], true
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue