Swift: String contains String (Without using NSString)?

后端 未结 4 1686
逝去的感伤
逝去的感伤 2020-12-19 02:16

I have the same problem like in this question:

How do I check if a string contains another string in Swift?

But now a few months later I wonder if it can be

4条回答
  •  醉话见心
    2020-12-19 03:00

    String actually provides a "contains" function through StringProtocol.
    No extension whatsoever needed:

    let str = "asdf"
    print(str.contains("sd") ? "yep" : "nope")
    

    https://developer.apple.com/reference/swift/string https://developer.apple.com/documentation/swift/stringprotocol


    If you want to check if your string matches a specific pattern, I can recommend the NSHipster article about NSRegularExpressions: http://nshipster.com/nsregularexpression/

提交回复
热议问题