mirror of
https://github.com/golang/go
synced 2024-11-21 11:44:43 -07:00
strings: add strings helpers for prefixes and suffixes
Signed-off-by: guoguangwu <guoguangwug@gmail.com>
This commit is contained in:
parent
69583738eb
commit
1174efb5d6
@ -172,6 +172,15 @@ func ExampleHasPrefix() {
|
||||
// true
|
||||
}
|
||||
|
||||
func ExampleHasAnyOfPrefixes() {
|
||||
fmt.Println(strings.HasAnyOfPrefixes("Gopher", "Go", "pher"))
|
||||
fmt.Println(strings.HasAnyOfPrefixes("Gopher", "C", "D"))
|
||||
fmt.Println(strings.HasAnyOfPrefixes("Gopher", ""))
|
||||
// Output:
|
||||
// true
|
||||
// false
|
||||
// true
|
||||
}
|
||||
func ExampleHasSuffix() {
|
||||
fmt.Println(strings.HasSuffix("Amigo", "go"))
|
||||
fmt.Println(strings.HasSuffix("Amigo", "O"))
|
||||
@ -184,6 +193,17 @@ func ExampleHasSuffix() {
|
||||
// true
|
||||
}
|
||||
|
||||
func ExampleHasAnyOfSuffixes() {
|
||||
fmt.Println(strings.HasAnyOfSuffixes("Amigo", "go", "Am"))
|
||||
fmt.Println(strings.HasAnyOfSuffixes("Amigo", "O", "p"))
|
||||
fmt.Println(strings.HasAnyOfSuffixes("Amigo", "Ami", "Am"))
|
||||
fmt.Println(strings.HasAnyOfSuffixes("Amigo", ""))
|
||||
// Output:
|
||||
// true
|
||||
// false
|
||||
// false
|
||||
// true
|
||||
}
|
||||
func ExampleIndex() {
|
||||
fmt.Println(strings.Index("chicken", "ken"))
|
||||
fmt.Println(strings.Index("chicken", "dmr"))
|
||||
|
@ -468,6 +468,28 @@ func HasSuffix(s, suffix string) bool {
|
||||
return len(s) >= len(suffix) && s[len(s)-len(suffix):] == suffix
|
||||
}
|
||||
|
||||
// HasAnyOfSuffixes returns true if the given string has any of the given suffixes.
|
||||
func HasAnyOfSuffixes(input string, suffixes ...string) bool {
|
||||
for _, suffix := range suffixes {
|
||||
if HasSuffix(input, suffix) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// HasAnyOfPrefixes returns true if the given string has any of the given prefixes.
|
||||
func HasAnyOfPrefixes(input string, prefixes ...string) bool {
|
||||
for _, prefix := range prefixes {
|
||||
if HasPrefix(input, prefix) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// Map returns a copy of the string s with all its characters modified
|
||||
// according to the mapping function. If mapping returns a negative value, the character is
|
||||
// dropped from the string with no replacement.
|
||||
|
Loading…
Reference in New Issue
Block a user