github.com/gogf/gf@v1.16.9/text/gstr/gstr_str.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gstr 8 9 import "strings" 10 11 // Str returns part of `haystack` string starting from and including 12 // the first occurrence of `needle` to the end of `haystack`. 13 // See http://php.net/manual/en/function.strstr.php. 14 func Str(haystack string, needle string) string { 15 if needle == "" { 16 return "" 17 } 18 pos := strings.Index(haystack, needle) 19 if pos == NotFoundIndex { 20 return "" 21 } 22 return haystack[pos+len([]byte(needle))-1:] 23 } 24 25 // StrEx returns part of `haystack` string starting from and excluding 26 // the first occurrence of `needle` to the end of `haystack`. 27 func StrEx(haystack string, needle string) string { 28 if s := Str(haystack, needle); s != "" { 29 return s[1:] 30 } 31 return "" 32 } 33 34 // StrTill returns part of `haystack` string ending to and including 35 // the first occurrence of `needle` from the start of `haystack`. 36 func StrTill(haystack string, needle string) string { 37 pos := strings.Index(haystack, needle) 38 if pos == NotFoundIndex || pos == 0 { 39 return "" 40 } 41 return haystack[:pos+1] 42 } 43 44 // StrTillEx returns part of `haystack` string ending to and excluding 45 // the first occurrence of `needle` from the start of `haystack`. 46 func StrTillEx(haystack string, needle string) string { 47 pos := strings.Index(haystack, needle) 48 if pos == NotFoundIndex || pos == 0 { 49 return "" 50 } 51 return haystack[:pos] 52 }