How to check if there is a special character in string or if a character is a special character in GoLang

huangapple go评论94阅读模式
英文:

How to check if there is a special character in string or if a character is a special character in GoLang

问题

从输入中读取一个字符串后,我需要检查其中是否有特殊字符。

英文:

After reading a string from the input, I need to check if there is a special character in it

答案1

得分: 17

你可以使用strings.ContainsAny函数来判断是否存在一个rune字符:

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. func main() {
  7. fmt.Println(strings.ContainsAny("Hello World", ",|"))
  8. fmt.Println(strings.ContainsAny("Hello, World", ",|"))
  9. fmt.Println(strings.ContainsAny("Hello|World", ",|"))
  10. }

或者,如果你想检查是否只有ASCII字符,你可以使用strings.IndexFunc函数:

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. func main() {
  7. f := func(r rune) bool {
  8. return r < 'A' || r > 'z'
  9. }
  10. if strings.IndexFunc("HelloWorld", f) != -1 {
  11. fmt.Println("找到特殊字符")
  12. }
  13. if strings.IndexFunc("Hello World", f) != -1 {
  14. fmt.Println("找到特殊字符")
  15. }
  16. }
英文:

You can use strings.ContainsAny to see if a rune exists:

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;strings&quot;
  5. )
  6. func main() {
  7. fmt.Println(strings.ContainsAny(&quot;Hello World&quot;, &quot;,|&quot;))
  8. fmt.Println(strings.ContainsAny(&quot;Hello, World&quot;, &quot;,|&quot;))
  9. fmt.Println(strings.ContainsAny(&quot;Hello|World&quot;, &quot;,|&quot;))
  10. }

Or if you want to check if there are only ASCII characters, you can use strings.IndexFunc:

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;strings&quot;
  5. )
  6. func main() {
  7. f := func(r rune) bool {
  8. return r &lt; &#39;A&#39; || r &gt; &#39;z&#39;
  9. }
  10. if strings.IndexFunc(&quot;HelloWorld&quot;, f) != -1 {
  11. fmt.Println(&quot;Found special char&quot;)
  12. }
  13. if strings.IndexFunc(&quot;Hello World&quot;, f) != -1 {
  14. fmt.Println(&quot;Found special char&quot;)
  15. }
  16. }

答案2

得分: 3

根据你对“特殊字符”的定义,最简单的解决方案可能是在字符串上进行for range循环(产生的是符文而不是字节),并且对于每个符文,检查它是否在你的允许/禁止符文列表中。

有关字符串、字节和符文之间的关系,请参阅Go中的字符串、字节、符文和字符

Playground示例

  1. package main
  2. var allowed = []rune{'a','b','c','d','e','f','g'}
  3. func haveSpecial(input string) bool {
  4. for _, char := range input {
  5. found := false
  6. for _, c := range allowed {
  7. if c == char {
  8. found = true
  9. break
  10. }
  11. }
  12. if !found {
  13. return true
  14. }
  15. }
  16. return false
  17. }
  18. func main() {
  19. cases := []string{
  20. "abcdef",
  21. "abc$€f",
  22. }
  23. for _, input := range cases {
  24. if haveSpecial(input) {
  25. println(input + ": NOK")
  26. } else {
  27. println(input + ": OK")
  28. }
  29. }
  30. }
英文:

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.

See Strings, bytes, runes and characters in Go for more about the relations between string, bytes and runes.

Playground example

  1. package main
  2. var allowed = []rune{&#39;a&#39;,&#39;b&#39;,&#39;c&#39;,&#39;d&#39;,&#39;e&#39;,&#39;f&#39;,&#39;g&#39;}
  3. func haveSpecial(input string) bool {
  4. for _, char := range input {
  5. found := false
  6. for _, c := range allowed {
  7. if c == char {
  8. found = true
  9. break
  10. }
  11. }
  12. if !found {
  13. return true
  14. }
  15. }
  16. return false
  17. }
  18. func main() {
  19. cases := []string{
  20. &quot;abcdef&quot;,
  21. &quot;abc$f&quot;,
  22. }
  23. for _, input := range cases {
  24. if haveSpecial(input) {
  25. println(input + &quot;: NOK&quot;)
  26. } else {
  27. println(input + &quot;: OK&quot;)
  28. }
  29. }
  30. }

答案3

得分: -1

你想使用unicode包,它有一个很好的函数可以检查符号。

https://golang.org/pkg/unicode/#IsSymbol

  1. package main
  2. import (
  3. "fmt"
  4. "unicode"
  5. )
  6. func hasSymbol(str string) bool {
  7. for _, letter := range str {
  8. if unicode.IsSymbol(letter) {
  9. return true
  10. }
  11. }
  12. return false
  13. }
  14. func main() {
  15. var strs = []string {
  16. "A quick brown fox",
  17. "A+quick_brown&lt;fox",
  18. }
  19. for _, str := range strs {
  20. if hasSymbol(str) {
  21. fmt.Printf("字符串 '%v' 包含符号。\n", str)
  22. } else {
  23. fmt.Printf("字符串 '%v' 不包含符号。\n", str)
  24. }
  25. }
  26. }

这将提供以下输出:

  1. 字符串 'A quick brown fox' 不包含符号。
  2. 字符串 'A+quick_brown&lt;fox' 包含符号。
英文:

You want to use the unicode package, which has a nice function to check for symbols.

https://golang.org/pkg/unicode/#IsSymbol

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;unicode&quot;
  5. )
  6. func hasSymbol(str string) bool {
  7. for _, letter := range str {
  8. if unicode.IsSymbol(letter) {
  9. return true
  10. }
  11. }
  12. return false
  13. }
  14. func main() {
  15. var strs = []string {
  16. &quot;A quick brown fox&quot;,
  17. &quot;A+quick_brown&lt;fox&quot;,
  18. }
  19. for _, str := range strs {
  20. if hasSymbol(str) {
  21. fmt.Printf(&quot;String &#39;%v&#39; contains symbols.\n&quot;, str)
  22. } else {
  23. fmt.Printf(&quot;String &#39;%v&#39; did not contain symbols.\n&quot;, str)
  24. }
  25. }
  26. }

This will provide the following output:

  1. String &#39;A quick brown fox&#39; did not contain symbols.
  2. String &#39;A+quick_brown&lt;fox&#39; contains symbols.

答案4

得分: -2

我最终做了这样的事情:

  1. alphabet := "abcdefghijklmnopqrstuvwxyz"
  2. alphabetSplit := strings.Split(alphabet, "")
  3. inputLetters := strings.Split(input, "")
  4. for index, value := range inputLetters {
  5. special := 1
  6. for _, char := range alphabetSplit {
  7. if char == value {
  8. special = 0
  9. break
  10. }
  11. }
英文:

I ended up doing something like this

  1. alphabet := &quot;abcdefghijklmnopqrstuvwxyz&quot;
  2. alphabetSplit := strings.Split(alphabet, &quot;&quot;)
  3. inputLetters := strings.Split(input, &quot;&quot;)
  4. for index, value := range inputLetters {
  5. special:=1
  6. for _, char :=range alphabetSplit{
  7. if char == value {
  8. special = 0
  9. break
  10. }
  11. }

It might have anything wrong because since I used it to something specific i had to edit to post it here

huangapple
  • 本文由 发表于 2015年8月12日 17:57:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/31961882.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定