Add substring funcs

This commit is contained in:
世界 2022-08-15 22:36:45 +08:00
parent 6b313ff9ef
commit 7868451c90
No known key found for this signature in database
GPG key ID: CD109927C34A63C4

35
common/string.go Normal file
View file

@ -0,0 +1,35 @@
package common
import "strings"
func SubstringAfter(s string, substr string) string {
index := strings.Index(s, substr)
if index == -1 {
return s
}
return s[index+len(substr):]
}
func SubstringAfterLast(s string, substr string) string {
index := strings.LastIndex(s, substr)
if index == -1 {
return s
}
return s[index+len(substr):]
}
func SubstringBefore(s string, substr string) string {
index := strings.Index(s, substr)
if index == -1 {
return s
}
return s[:index]
}
func SubstringBeforeLast(s string, substr string) string {
index := strings.LastIndex(s, substr)
if index == -1 {
return s
}
return s[:index]
}