Go StartsWith(str string)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12667327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 01:37:21  来源:igfitidea点击:

Go StartsWith(str string)

stringgostartswith

提问by Ammar

Is there a StartsWith(str1, str2 string)function that can check if str1is a prefix of str2in Go language?

是否有一个StartsWith(str1, str2 string)函数可以检查Go 语言中str1的前缀是否为str2

I want a function similar to the Java's startsWith().

我想要一个类似于Java 的startsWith().

回答by Jeremy Wall

The strings package has what you are looking for. Specifically the HasPrefix function: http://golang.org/pkg/strings/#HasPrefix

字符串包有你要找的东西。特别是 HasPrefix 函数:http://golang.org/pkg/strings/#HasPrefix

Example:

例子:

fmt.Println(strings.HasPrefix("my string", "prefix"))  // false
fmt.Println(strings.HasPrefix("my string", "my"))      // true

That package is full of a lot of different string helper functions you should check out.

那个包里有很多不同的字符串辅助函数,你应该看看。