Go语言判断字符串是否包含特定子字符串
可以使用 strings.Contains() 函数来判断字符串中是否包含指定的子字符串。以下是一个示例代码:
package main
import (
"fmt"
"strings"
)
func main() {
contactForm := struct {
Number string
}{
Number: "12345RW6789",
}
if strings.Contains(contactForm.Number, 'RW') || strings.Contains(contactForm.Number, 'LXD') {
fmt.Println("contactForm.Number 包含 RW 或者 LXD")
} else {
fmt.Println("contactForm.Number 不包含 RW 或者 LXD")
}
}
在上述代码中,我们使用 strings.Contains() 函数来检查 contactForm.Number 字符串是否包含 'RW' 或者 'LXD' 子字符串。如果包含,则输出 "contactForm.Number 包含 RW 或者 LXD",否则输出 "contactForm.Number 不包含 RW 或者 LXD"。
原文地址: https://www.cveoy.top/t/topic/pZbL 著作权归作者所有。请勿转载和采集!