×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

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

Parameters

  • 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("__@Okay123", "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 »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.