You can use .ContainsAny lines to see if a rune exists:
package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.ContainsAny("Hello World", ",|")) fmt.Println(strings.ContainsAny("Hello, World", ",|")) fmt.Println(strings.ContainsAny("Hello|World", ",|")) }
Or, if you want to check if there are only ASCII characters, you can use .IndexFunc lines:
package main import ( "fmt" "strings" ) func main() { f := func(r rune) bool { return r < 'A' || r > 'z' } if strings.IndexFunc("HelloWorld", f) != -1 { fmt.Println("Found special char") } if strings.IndexFunc("Hello World", f) != -1 { fmt.Println("Found special char") } }
source share