Golang程序 检查一个字符串是否包含子串
子串是字符串中的一个小字符串,Golang中的字符串是一个字符的集合。由于Go中的字符串是不可改变的,所以在产生后不能修改。然而,串联或添加到现有的字符串中,可以创建新的字符串。作为Go中的内置类型,字符串类型可以像其他数据类型一样以各种方式使用。
语法
strings.Contains(str,substring string)
要确定一个字符串是否包含一个特定的子串,可以使用Contains(s, substr string) bool函数。如果在所提供的字符串中发现了该子串,那么将返回一个表明其存在的布尔值。
strings.Index(str, substring string)
int函数index(s, str string)用于确定给定字符串中指定子串的第一个实例的索引。如果子串丢失,它将返回-1,或者返回子串在字符串中的索引。
strings.Index(str, substring string)
int函数index(s, str string)用于确定一个给定的字符串中指定子串的第一个实例的索引。如果子串丢失,它将返回-1,或者返回子串在字符串中的索引。
算法
- 第1步 – 创建一个包main并声明fmt(format包)和strings包
-
第2步 – 创建一个函数main,并在该函数中创建一个字符串mystr
-
第3步 – 使用字符串函数,检查该字符串是否包含子串
-
第4步 – 打印输出结果
例子1
在这个例子中,我们将看到如何使用内置函数strings.Contains()来检查一个字符串是否包含一个子串。输出将是一个打印在控制台的布尔值。让我们通过代码和算法来了解一下这个概念。
package main
import (
"fmt"
"strings"
)
func main() {
mystr := "Hello,alexa!" //create string
fmt.Println("The string created here is:", mystr)
substring := "alexa" //hold substring
fmt.Println("The substring from the string is:", substring)
fmt.Println("Whether the substring is present in string or not?")
fmt.Println(strings.Contains(mystr, substring)) //use built-in function to check if substring is present in string
}
输出
The string created here is: Hello,alexa!
The substring from the string is: alexa
Whether the substring is present in string or not?
true
例子2
在这个例子中,我们将看到如何使用strings.Index()函数来检查一个字符串是否包含一个子串。
package main
import (
"fmt"
"strings"
)
func main() {
mystr := "Hello, alexa!" //create string
fmt.Println("The string created here is:", mystr)
substring := "alexa" //substring
fmt.Println("The substring from the string is:", substring)
fmt.Println("Whether the string contains the substring or not?")
if strings.Index(mystr, substring) >= 0 { //check if condition is true print the statement
fmt.Println("The string contains the substring.")
} else {
fmt.Println("The string does not contain the substring.")
}
}
输出
The string created here is: Hello, alexa!
The substring from the string is: alexa
Whether the string contains the substring or not?
The string contains the substring.
例子3
在这个例子中,我们将看到如何使用strings.Index()函数中的for循环来查找一个字符串是否包含一个子串。
package main
import (
"fmt"
"strings"
)
func main() {
mystr := "Hello, alexa!" //create string
fmt.Println("The string created here is:", mystr)
substring := "alexa" //hold substring
fmt.Println("The substring present here is:", substring)
fmt.Println("Whether the substring is present in string or not?")
found := false
for i := 0; i < len(mystr); i++ {
if strings.Index(mystr[i:], substring) == 0 { //check if substring is present in string or not
found = true
break
}
}
if found {
fmt.Println("The string has substring in it.")
} else {
fmt.Println("The string does not have substring in it.")
}
}
输出
The string created here is: Hello, alexa!
The substring present here is: alexa
Whether the substring is present in string or not?
The string has substring in it.
总结
我们通过三个例子执行了检查一个字符串是否包含一个子串的程序。在第一个例子中,我们使用了strings.Contains()函数,在第二个例子中,我们使用了strings.Index()函数,在第三个例子中,我们使用了for循环和前一个内置函数。