Make a note of how to check if a variable like the one below contains a particular string
package main
func main(){
str := "abcde"
}
Basically you will use the strings
package, but there are two ways
strings.Index
It is a method to check the character number of a specific character string, but if the specific character string is not included, it returns -1
, so use that.
package main
import (
"fmt"
"strings"
)
func main(){
str := "abcde"
fmt.Print(strings.Index(str, "fg"))
//result:-1
}
strings.Contains
I introduced strings.Index, but I think that I will basically use this because it will be returned as a bool.
package main
import (
"fmt"
"strings"
)
func main(){
str := "abcde"
fmt.Print(strings.Contains(str, "fg"))
//Result: false
}
Recommended Posts