10

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 done without using NSString? I nice and simple contains-method would be fine. I searched the web and the documentation but I found nothing!

Community
  • 1
  • 1
TalkingCode
  • 13,407
  • 27
  • 102
  • 147
  • 1
    Possible duplicate of [How do I check if a string contains another string in Swift?](http://stackoverflow.com/questions/24034043/how-do-i-check-if-a-string-contains-another-string-in-swift) – Max MacLeod Aug 22 '16 at 09:16

4 Answers4

28

Same way, just with Swift syntax:

let string = "This is a test.  This is only a test"

if string.rangeOfString("only") != nil {
     println("yes")
}

For Swift 3.0

if str.range(of: "abc") != nil{
     print("Got the string")
}
Patel Jigar
  • 2,141
  • 1
  • 23
  • 30
Steve Rosenberg
  • 19,348
  • 7
  • 46
  • 53
  • 1
    what about "is", can i get the last "is"? – Zam Mar 24 '15 at 07:18
  • @Zam In that case, it will returns the first coincidence/match for `is`, and if you print the `range` you will get something like `2..<4` {^_^} --> `if let range = string.rangeOfString("is") { print(range) }` – eMdOS Dec 10 '15 at 16:22
  • Is that syntax still used in Swift 2 ? – Jack Berstrem Dec 14 '15 at 22:49
  • 5
    `rangeOfString` is an `NSString` method. – mxcl Dec 15 '15 at 23:28
  • @mxcl CMD+Click the method in Xcode und you will see that Swift's String has an own implementation of rangeOfString. Testing for != nil wouldn't work with the NSString implementation btw. – Thyraz Jan 18 '16 at 19:50
  • 2
    @Thyraz it's not: https://github.com/apple/swift/blob/master/stdlib/public/core/String.swift – mxcl Jan 19 '16 at 21:57
  • Nice and simple. I like it. – Edward May 24 '16 at 06:23
  • Please use consistent examples for Swift2/Swift3. And mention that `range(of:)` is part of `Foundation`, so you need to import that. – Tali Dec 22 '16 at 07:46
3

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

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

enter image description here

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/

d.felber
  • 5,288
  • 1
  • 21
  • 36
  • That's check if the string contains а certain character! – Emil Marashliev Nov 26 '17 at 21:50
  • 1
    @EmilMarashliev That is true. But additionally you can check anything that conforms to the `StringProtocol` - so it also works for `String`. – d.felber Nov 27 '17 at 07:07
  • Ha, you're completely right it comes from `StringProtocol` https://developer.apple.com/documentation/swift/stringprotocol/2923423-contains it's more convenient and elegant way than `range(of:)` – Emil Marashliev Nov 27 '17 at 09:13
0

I wrote an extension on String for SWIFT 3.0 so that i could simply call absoluteString.contains(string: "/kredit/")

extension String {
  public func contains(string: String)-> Bool {
      return self.rangeOfString(string) != nil
  }

}

Ilker Baltaci
  • 11,644
  • 6
  • 63
  • 79
0

just to demonstrate the use of options.

var string = "This is a test.  This is only a test. Not an Exam"

if string.range(of:"ex") != nil {
    print("yes")
}
if string.range(of:"ex", options: String.CompareOptions.caseInsensitive) != nil {
    print("yes")
}
nyxee
  • 2,773
  • 26
  • 22