[英]How to check if there is a special character in string or if a character is a special character in GoLang
从输入中读取字符串后,我需要检查其中是否有特殊字符
You can use strings.ContainsAny to see if a rune exists:您可以使用 strings.ContainsAny 查看符文是否存在:
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 strings.IndexFunc:或者如果你想检查是否只有 ASCII 字符,你可以使用 strings.IndexFunc:
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")
}
}
Depending on your definition of special character , the simplest solution would probably to do a for range
loop on your string (which yield runes instead of bytes), and for each rune check if it is in your list of allowed/forbidden runes.根据您对special character的定义,最简单的解决方案可能是对您的字符串执行for range
循环(产生符文而不是字节),并为每个符文检查它是否在您的允许/禁止符文列表中。
See Strings, bytes, runes and characters in Go for more about the relations between string, bytes and runes.有关字符串、字节和符文之间关系的更多信息,请参阅Go 中的字符串、字节、符文和字符。
package main
var allowed = []rune{'a','b','c','d','e','f','g'}
func haveSpecial(input string) bool {
for _, char := range input {
found := false
for _, c := range allowed {
if c == char {
found = true
break
}
}
if !found {
return true
}
}
return false
}
func main() {
cases := []string{
"abcdef",
"abc$€f",
}
for _, input := range cases {
if haveSpecial(input) {
println(input + ": NOK")
} else {
println(input + ": OK")
}
}
}
You want to use the unicode package, which has a nice function to check for symbols.您想使用 unicode 包,它有一个很好的功能来检查符号。
https://golang.org/pkg/unicode/#IsSymbol https://golang.org/pkg/unicode/#IsSymbol
package main
import (
"fmt"
"unicode"
)
func hasSymbol(str string) bool {
for _, letter := range str {
if unicode.IsSymbol(letter) {
return true
}
}
return false
}
func main() {
var strs = []string {
"A quick brown fox",
"A+quick_brown<fox",
}
for _, str := range strs {
if hasSymbol(str) {
fmt.Printf("String '%v' contains symbols.\n", str)
} else {
fmt.Printf("String '%v' did not contain symbols.\n", str)
}
}
}
This will provide the following output:这将提供以下输出:
String 'A quick brown fox' did not contain symbols.
String 'A+quick_brown<fox' contains symbols.
I ended up doing something like this我最终做了这样的事情
alphabet := "abcdefghijklmnopqrstuvwxyz"
alphabetSplit := strings.Split(alphabet, "")
inputLetters := strings.Split(input, "")
for index, value := range inputLetters {
special:=1
for _, char :=range alphabetSplit{
if char == value {
special = 0
break
}
}
It might have anything wrong because since I used it to something specific i had to edit to post it here它可能有什么问题,因为因为我将它用于特定的东西,所以我必须编辑才能将其发布在这里
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.