HasPrefix()
Published Aug 29, 2023
Contribute to Docs
The HasPrefix()
function returns a boolean value indicating whether a given string begins with a given prefix. The method returns true
if the string begins with the prefix, otherwise it returns false
.
Syntax
strings.HasPrefix(str, prefix)
Where str
is the string where we want to check the prefix and prefix
is the prefix to be checked.
Example
The following code demonstrate the use of the strings.HasPrefix()
function.
package mainimport ("fmt""strings")func main() {fmt.Println(strings.HasPrefix("codecademy","code"))fmt.Println(strings.HasPrefix("GitHub","Hub"))}
The output will be:
truefalse
Codebyte Example
The following example is runnable and uses the strings.HasPrefix()
function to determine if codecademy
begins with code
.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Go on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Go
Learn how to use Go (Golang), an open-source programming language supported by Google!Beginner Friendly6 hours