Home  >  Article  >  Backend Development  >  How to determine whether it ends with the specified character in golang

How to determine whether it ends with the specified character in golang

青灯夜游
青灯夜游Original
2023-01-13 10:13:334463browse

In golang, you can use the HasSuffix() function of the strings package to determine whether a string ends with a specified character or string. The syntax is "strings.HasSuffix (original string, substring to be retrieved)"; Returns True if it ends with the specified character, False if not.

How to determine whether it ends with the specified character in golang

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

In the development process, many times we need to determine whether a string ends with a certain character or whether it ends with a certain string. In the Go language, it is necessary to determine whether a string ends with a certain character. Or whether the function that ends with a string is Strings.HasSuffix().

Usage example:

package main
import (
    "fmt"
    "strings"
)
func main() {
	//使用 Strings.HasSuffix() 函数,判断以指定字符串结束,返回 true
	strHaiCoder := "I study Golang From HaiCoder"
	suffix := strings.HasSuffix(strHaiCoder, "HaiCoder")
	fmt.Println("Suffix =", suffix)
}

How to determine whether it ends with the specified character in golang

As you can see, the string given in the above example ends with "HaiCoder". Therefore returns true.

Description: Introduction to the Strings.HasSuffix() function

The Strings.HasSuffix() function is used to retrieve whether a string ends with a specified string

Syntax:

func HasSuffix(s, suffix string) bool
Parameter Description
s Original string.
suffix The substring to be retrieved.

Return value: If it ends with the specified string, it returns true; otherwise, it returns false.

Use the Strings.HasSuffix() function to determine whether it ends with the specified string. Alternatively, you can also determine whether it does not end with the specified string.

package main
import (
    "fmt"
    "strings"
)
func main() {
	//使用 Strings.HasSuffix() 函数,判断不以指定字符串结束,返回 Fasle
	strHaiCoder := "I study Golang From HaiCoder"
	suffix := strings.HasSuffix(strHaiCoder, "Golang")
	fmt.Println("Suffix =", suffix)
}

How to determine whether it ends with the specified character in golang

Analysis: We define a string type variable strHaicoder, and then we use the string strings.HasSuffix() function to determine whether the variable strHaicoder is the string "Golang ” ends and the result returns False.

【Related recommendations: Go video tutorial, Programming teaching

The above is the detailed content of How to determine whether it ends with the specified character in golang. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn