golang判断字符是否存在字符串中的方法:
判断子字符串或字符在父字符串中出现的位置(索引)
Index 返回字符串 str 在字符串 s 中的索引( str 的第一个字符的索引),-1 表示字符串 s 不包含字符串 str :
strings.Index(s, str string) int
LastIndex 返回字符串 str 在字符串 s 中最后出现位置的索引( str 的第一个字符的索引),-1 表示字符串 s 不包含字符串 str :
strings.LastIndex(s, str string) int
如果 ch 是非 ASCII 编码的字符,建议使用以下函数来对字符进行定位:
strings.IndexRune(s string, ch int) int
示例:
package main import ( "fmt" "strings" ) func main() { var str string = "Hi, I'm Marc, Hi." fmt.Printf("The position of \"Marc\" is: ") fmt.Printf("%d\n", strings.Index(str, "Marc")) fmt.Printf("The position of the first instance of \"Hi\" is: ") fmt.Printf("%d\n", strings.Index(str, "Hi")) fmt.Printf("The position of the last instance of \"Hi\" is: ") fmt.Printf("%d\n", strings.LastIndex(str, "Hi")) fmt.Printf("The position of \"Burger\" is: ") fmt.Printf("%d\n", strings.Index(str, "Burger")) }
以上就是golang判断字符是否存在字符串中的详细内容,更多请关注创新互联成都网站设计公司其它相关文章!
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。