在 Go 中,没有内置的 contains 方法来检查一个字符串是否包含另一个字符串。但是,可以使用 strings 包中的 Contains 函数来实现这个功能。下面是一个示例:

package main

import (
	"fmt"
	"strings"
)

func contains(s, substr string) bool {
	return strings.Contains(s, substr)
}

func main() {
	s := 'Hello, World!'
	substr := 'World'

	if contains(s, substr) {
		fmt.Printf('%s contains %s\n', s, substr)
	} else {
		fmt.Printf('%s does not contain %s\n', s, substr)
	}
}

输出结果:

Hello, World! contains World
Golang 字符串包含判断:使用 strings.Contains 方法

原文地址: http://www.cveoy.top/t/topic/biF8 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录