ADVERTISEMENT
ADVERTISEMENT

Golang strings.HasSuffix() Function with Examples

Golang | strings.HasSuffix() Function: Here, we are going to learn about the HasSuffix() function of strings package with its usages, syntax, and examples.
Submitted by IncludeHelp, on August 16, 2021

strings.HasSuffix()

The HasSuffix() function is an inbuilt function of strings package which is used to check whether a string ends with a given suffix. It accepts a string and suffix (substring) and returns true if the string ends with suffix, false otherwise.

Syntax:

func HasSuffix(str, suffix string) bool

Parameter(s):

  • str : String in which we want to check the suffix.
  • prefix : Suffix to be checked

Return Value:

The return type of HasSuffix() function is a bool, it returns true if the string ends with suffix, false otherwise.

Example 1:

// Golang program to demonstrate the
// example of strings.HasSuffix() Function

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.HasSuffix("Hello, world!", "!"))
	fmt.Println(strings.HasSuffix("[email protected]", "123"))
	fmt.Println(strings.HasSuffix("Hello, world", "."))
}

Output:

true
true
false

Example 2:

// Golang program to demonstrate the
// example of strings.HasSuffix() Function

package main

import (
	"fmt"
	"strings"
)

func main() {
	var str string
	var suffix string

	str = "Hello, world! How are you?"
	suffix = "?"

	if strings.HasSuffix(str, suffix) == true {
		fmt.Printf("%q ends with %q\n", str, suffix)
	} else {
		fmt.Printf("%q does not end with %q\n", str, suffix)
	}

	str = "Greetings from IncludeHelp.?"
	suffix = ".?"

	if strings.HasSuffix(str, suffix) == true {
		fmt.Printf("%q ends with %q\n", str, suffix)
	} else {
		fmt.Printf("%q does not end with %q\n", str, suffix)
	}

	str = "Hello, world! How are you?"
	suffix = "."

	if strings.HasSuffix(str, suffix) == true {
		fmt.Printf("%q ends with %q\n", str, suffix)
	} else {
		fmt.Printf("%q does not end with %q\n", str, suffix)
	}
}

Output:

"Hello, world! How are you?" ends with "?"
"Greetings from IncludeHelp.?" ends with ".?"
"Hello, world! How are you?" does not end with "."

Golang strings Package Functions »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.