douchuitang0331 2019-01-15 18:10
浏览 33
已采纳

如何检查字符串中的一个或多个符号

I'm struggling to find a method of checking to see if a string contains one or more symbols like the following: #}{&*"(£) While it's fairly trivial to make a check for one or more of these symbols I'm not looking to build and maintain a list or dictionary of possible entries. Is there a way I can check to see if a string contains one or more of any non standard symbol in go preferably using the standard library?

Specifically I'm looking to detect anything that isn't a-zA-Z0-9 which for the basis of my question would be counted as a non standard symbol.

  • 写回答

2条回答 默认 最新

  • douxia1988 2019-01-15 18:51
    关注

    In Go, write a simple function. For example,

    package main
    
    import (
        "fmt"
    )
    
    func isStandard(s string) bool {
        for i := 0; i < len(s); i++ {
            switch b := s[i]; {
            case b >= 'a' && b <= 'z':
                continue
            case b >= 'A' && b <= 'Z':
                continue
            case b >= '0' && b <= '9':
                continue
            default:
                return false
            }
        }
        return true
    }
    
    func main() {
        fmt.Println(isStandard(`ABCabc123`))
        fmt.Println(isStandard(`#}{&*"(£)`))
    }
    

    Playground: https://play.golang.org/p/Y2KjDcHSupH

    Output:

    true
    false
    

    The Go Programming Language Specification

    Switch statements

    "Switch" statements provide multi-way execution. An expression or type specifier is compared to the "cases" inside the "switch" to determine which branch to execute.

    Expression switches

    In an expression switch, the switch expression is evaluated and the case expressions, which need not be constants, are evaluated left-to-right and top-to-bottom; the first one that equals the switch expression triggers execution of the statements of the associated case; the other cases are skipped. If no case matches and there is a "default" case, its statements are executed. There can be at most one default case and it may appear anywhere in the "switch" statement.

    The switch expression may be preceded by a simple statement, which executes before the expression is evaluated.

    Fallthrough statements

    A "fallthrough" statement transfers control to the first statement of the next case clause in an expression "switch" statement. It may be used only as the final non-empty statement in such a clause.


    switch b := s[i]; {
        // ...
    }
    

    is equivalent to

    switch b := s[i]; true {
        // ...
    }
    

    is equivalent to

    {
        b := s[i]
        switch true {
            // ...
        } 
    }
    

    The simple statement b := s[i] declares b to be a switch { } statement block local variable.

    The case expresions are evaluated and compared to true. If none are true, the default is taken.

    Go, unlike C, requires explicit fallthrough.

    ASCII is a subset of Unicode UTF-8. Since the standard characters are all ASCII, we can simply compare bytes.


    Here is a simple benchmark.

    Output:

    $ go test standard_test.go -bench=. -benchmem
    BenchmarkPeterSO-8    200000000       8.10 ns/op    0 B/op    0 allocs/op
    BenchmarkJubobs-8      10000000     222 ns/op       0 B/op    0 allocs/op
    $ 
    

    standard_test.go:

    package main
    
    import (
        "regexp"
        "testing"
    )
    
    func isStandard(s string) bool {
        for i := 0; i < len(s); i++ {
            switch b := s[i]; {
            case b >= 'a' && b <= 'z':
                continue
            case b >= 'A' && b <= 'Z':
                continue
            case b >= '0' && b <= '9':
                continue
            default:
                return false
            }
        }
        return true
    }
    
    func BenchmarkPeterSO(b *testing.B) {
        std := `ABCabc123`
        for N := 0; N < b.N; N++ {
            isStandard(std)
        }
    }
    
    var (
        whitelist  = "A-Za-z0-9"
        disallowed = regexp.MustCompile("[^" + whitelist + " ]+")
    )
    
    func IsValid(s string) bool {
        return !disallowed.MatchString(s)
    }
    
    func BenchmarkJubobs(b *testing.B) {
        std := `ABCabc123`
        for N := 0; N < b.N; N++ {
            IsValid(std)
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算