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

后端 未结 27 3332
天命终不由人
天命终不由人 2020-11-22 12:32

In Objective-C the code to check for a substring in an NSString is:

NSString *string = @\"hello Swift\";
NSRange textRange =[strin         


        
相关标签:
27条回答
  • 2020-11-22 13:19

    > IN SWIFT 3.0

    let str = "Hello Swift"
    if str.lowercased().contains("Swift".lowercased()) {
        print("String Contains Another String")
    } else {
        print("Not Exists")
    }
    

    Output

    String Contains Another String
    
    0 讨论(0)
  • 2020-11-22 13:20

    Extension way

    Swift 4

    extension String {
        func contains(find: String) -> Bool{
            return self.range(of: find) != nil
        }
        func containsIgnoringCase(find: String) -> Bool{
            return self.range(of: find, options: .caseInsensitive) != nil
        }
    }
    
    var value = "Hello world"
    
    print(value.contains("Hello")) // true
    print(value.contains("bo"))    // false
    
    print(value.containsIgnoringCase(find: "hello"))    // true
    print(value.containsIgnoringCase(find: "Hello"))    // true
    print(value.containsIgnoringCase(find: "bo"))       // false
    

    Generally Swift 4 has contains method however it available from iOS 8.0+


    Swift 3.1

    You can write extension contains: and containsIgnoringCase for String

    extension String { 
    
       func contains(_ find: String) -> Bool{
         return self.range(of: find) != nil
       }
    
       func containsIgnoringCase(_ find: String) -> Bool{
         return self.range(of: find, options: .caseInsensitive) != nil 
       }
     }
    

    Older Swift version

    extension String {
    
        func contains(find: String) -> Bool{
           return self.rangeOfString(find) != nil
         }
    
        func containsIgnoringCase(find: String) -> Bool{
           return self.rangeOfString(find, options: NSStringCompareOptions.CaseInsensitiveSearch) != nil
         }
    }
    

    Example:

    var value = "Hello world"
    
    print(value.contains("Hello")) // true
    print(value.contains("bo"))    // false
    
    print(value.containsIgnoringCase("hello"))    // true
    print(value.containsIgnoringCase("Hello"))    // true
    print(value.containsIgnoringCase("bo"))       // false
    
    0 讨论(0)
  • 2020-11-22 13:20

    Of all of the answers here, I think they either don't work, or they're a bit of a hack (casting back to NSString). It's very likely that the correct answer to this has changed with the different beta releases.

    Here is what I use:

    let string: String = "hello Swift"
    if string.rangeOfString("Swift") != nil
    {
        println("exists")
    }
    

    The "!= nil" became required with Beta 5.

    0 讨论(0)
  • 2020-11-22 13:23

    As of Xcode 7.1 and Swift 2.1 containsString() is working fine for me.

    let string = "hello swift"
    if string.containsString("swift") {
        print("found swift")
    }
    

    Swift 4:

    let string = "hello swift"
    if string.contains("swift") {
        print("found swift")
    }
    

    And a case insensitive Swift 4 example:

    let string = "Hello Swift"
    if string.lowercased().contains("swift") {
        print("found swift")
    }
    

    Or using a case insensitive String extension:

    extension String {
        func containsIgnoreCase(_ string: String) -> Bool {
            return self.lowercased().contains(string.lowercased())
        }
    }
    
    let string = "Hello Swift"
    let stringToFind = "SWIFT"
    if string.containsIgnoreCase(stringToFind) {
        print("found: \(stringToFind)")  // found: SWIFT
    }
    print("string: \(string)")
    print("stringToFind: \(stringToFind)")
    
    // console output:
    found: SWIFT
    string: Hello Swift
    stringToFind: SWIFT
    
    0 讨论(0)
  • 2020-11-22 13:25

    Xcode 8/Swift 3 version:

    let string = "hello Swift"
    
    if let range = string.range(of: "Swift") {
        print("exists at range \(range)")
    } else {
        print("does not exist")
    }
    
    if let lowercaseRange = string.lowercased().range(of: "swift") {
        print("exists at range \(lowercaseRange)")
    } else {
        print("does not exist")
    }
    

    You can also use contains:

    string.contains("swift") // false
    string.contains("Swift") // true
    
    0 讨论(0)
  • 2020-11-22 13:25

    You can just do what you have mentioned:

    import Foundation
    ...
    string.contains("Swift");
    

    From the docs:

    Swift’s String type is bridged seamlessly to Foundation’s NSString class. If you are working with the Foundation framework in Cocoa or Cocoa Touch, the entire NSString API is available to call on any String value you create, in addition to the String features described in this chapter. You can also use a String value with any API that requires an NSString instance.

    You need to import Foundation to bridge the NSString methods and make them available to Swift's String class.

    0 讨论(0)
提交回复
热议问题