Use the strings.HasPrefix function in golang to determine whether a string starts with a certain prefix

王林
Release: 2023-11-18 12:18:41
Original
1517 people have browsed it

Use the strings.HasPrefix function in golang to determine whether a string starts with a certain prefix

Use the strings.HasPrefix function in golang to determine whether a string begins with a certain prefix

In golang, the strings.HasPrefix function can help us determine a string Whether to start with the specified prefix. I often use this function at work to determine whether a file path meets the requirements. The following is the specific usage and code example of this function.

Function introduction

Function name: HasPrefix
Function function: Determine whether the string s starts with prefix
Function parameters: s string - the string to be judged; prefix string - Prefix string
Function return value: bool - whether the string s begins with prefix

Function code example

The following is a sample code using this function:

package main

import (

"fmt"
"strings"
Copy after login

)

func main(){

str1 := "/usr/local/go/bin/go"
str2 := "/usr/local/go/src"
api1 := "/api/v1/user"
api2 := "/api/v2/user"

// 判断str1是否以指定的前缀"/usr"开头
if strings.HasPrefix(str1, "/usr"){
    fmt.Printf("str1以/usr开头
Copy after login

")

} else {
    fmt.Printf("str1不以/usr开头
Copy after login

")

}

// 判断str2是否以指定的前缀"/usr"开头
if strings.HasPrefix(str2, "/usr"){
    fmt.Printf("str2以/usr开头
Copy after login

")

} else {
    fmt.Printf("str2不以/usr开头
Copy after login

")

}

// 判断api1是否以指定的前缀"/api/v1"开头
if strings.HasPrefix(api1, "/api/v1"){
    fmt.Printf("api1以/api/v1开头
Copy after login

")

} else {
    fmt.Printf("api1不以/api/v1开头
Copy after login

")

}

// 判断api2是否以指定的前缀"/api/v1"开头
if strings.HasPrefix(api2, "/api/v1"){
    fmt.Printf("api2以/api/v1开头
Copy after login

")

} else {
    fmt.Printf("api2不以/api/v1开头
Copy after login

")

}
Copy after login

}

Output result:

str1 starts with /usr
str2 starts with /usr
api1 starts with /api/v1
api2 Not starting with /api/v1

As you can see from the output, the strings.HasPrefix function can easily determine whether a string starts with the specified prefix. In actual work, we can use this function flexibly according to business needs, thereby improving the readability and execution efficiency of the code.

The above is the detailed content of Use the strings.HasPrefix function in golang to determine whether a string starts with a certain prefix. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!