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

后端 未结 27 3330
天命终不由人
天命终不由人 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:11

    From the docs, it seems that calling containsString() on a String should work:

    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.

    However, it doesn't seem to work that way.

    If you try to use someString.containsString(anotherString), you will get a compile time error that states 'String' does not contain a member named 'containsString'.

    So, you're left with a few options, one of which is to explicitly bridge your String to Objective-C by using bridgeToObjectiveC() other two involve explicitly using an NSString and the final one involves casting the String to an NSString

    By bridging, you'd get:

    var string = "hello Swift"
    if string.bridgeToObjectiveC().containsString("Swift") {
        println("YES")
    }
    

    By explicitly typing the string as an NSString, you'd get:

    var string: NSString = "hello Swift"
    if string.containsString("Swift") {
        println("YES")
    }
    

    If you have an existing String, you can initialize an NSString from it by using NSString(string:):

    var string = "hello Swift"
    if NSString(string: string).containsString("Swift") {
        println("YES")
    }
    

    And finally, you can cast an existing String to an NSString as below

    var string = "hello Swift"
    if (string as NSString).containsString("Swift") {
        println("YES")
    }
    
    0 讨论(0)
  • 2020-11-22 13:11

    You can do this very easily in Swift using the code:

    let string = "hello Swift";
    let subString = (string as NSString).containsString("Swift")
    if(subString){println("Exist")}
    
    0 讨论(0)
  • 2020-11-22 13:11

    Check if it contains 'Hello'

    let s = "Hello World"
    
    if s.rangeOfString("Hello") != nil {
        print("Yes it contains 'Hello'")
    }
    
    0 讨论(0)
  • 2020-11-22 13:14

    Here you are:

    let s = "hello Swift"
    if let textRange = s.rangeOfString("Swift") {
        NSLog("exists")
    }
    
    0 讨论(0)
  • 2020-11-22 13:14

    With and new syntax in swift 4 you can just

    string.contains("Swift 4 is the best")

    string is your string variable

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

    Just an addendum to the answers here.

    You can also do a local case insensitive test using:

     - (BOOL)localizedCaseInsensitiveContainsString:(NSString *)aString
    

    Example:

        import Foundation
    
        var string: NSString  =  "hello Swift"
       if string.localizedCaseInsensitiveContainsString("Hello") {
        println("TRUE")
    }
    

    UPDATE

    This is part of the Foundation Framework for iOS & Mac OS X 10.10.x and was part of 10.10 at Time of my original Posting.

    Document Generated: 2014-06-05 12:26:27 -0700 OS X Release Notes Copyright © 2014 Apple Inc. All Rights Reserved.

    OS X 10.10 Release Notes Cocoa Foundation Framework

    NSString now has the following two convenience methods:

    - (BOOL)containsString:(NSString *)str;

    - (BOOL)localizedCaseInsensitiveContainsString:(NSString *)str;

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