HasSuffix()

jameskeezer5 total contributions
Published Sep 1, 2023
Contribute to Docs
The HasSuffix()
function in Go is used to check if a specified suffix exists on a given string. The function returns true
if the given string has the specified suffix, otherwise it returns false
.
Syntax
strings.HasSuffix(str, suffix)
Where str
is the given string and suffix
is the ending substring to check for. This function is case-sensitive.
Example
The following example demonstrates the use of the strings.HasSuffix()
function.
package mainimport ("fmt""strings")func main() {str := "Codecademy"firstSuffix := "demy"secondSuffix := "academy"fmt.Println(strings.HasSuffix(str, firstSuffix))fmt.Println(strings.HasSuffix(str, secondSuffix))}
The output will be:
truefalse
Codebyte Example
The following example is runnable and uses the strings.HasSuffix()
function to check if the given suffixes are present in the string Hello World
.
Looking to contribute?
- 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.