在Objective-C中,检查NSString子字符串的代码是:

NSString *string = @"hello Swift";
NSRange textRange =[string rangeOfString:@"Swift"];
if(textRange.location != NSNotFound)
{
    NSLog(@"exists");
}

但是如何在Swift中做到这一点呢?


当前回答

Xcode 8/Swift 3版本:

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")
}

你也可以使用contains:

string.contains("swift") // false
string.contains("Swift") // true

其他回答

另一个。支持大小写和变音符。

斯威夫特3.0

struct MyString {
  static func contains(_ text: String, substring: String,
                       ignoreCase: Bool = true,
                       ignoreDiacritic: Bool = true) -> Bool {

    var options = NSString.CompareOptions()

    if ignoreCase { _ = options.insert(NSString.CompareOptions.caseInsensitive) }
    if ignoreDiacritic { _ = options.insert(NSString.CompareOptions.diacriticInsensitive) }

    return text.range(of: substring, options: options) != nil
  }
}

使用

MyString.contains("Niels Bohr", substring: "Bohr") // true

iOS 9 +

iOS 9开始提供大小写和变音符不敏感功能。

if #available(iOS 9.0, *) {
  "Für Elise".localizedStandardContains("fur") // true
}

检查它是否包含“Hello”

let s = "Hello World"

if s.rangeOfString("Hello") != nil {
    print("Yes it contains 'Hello'")
}

SWIFT 4非常简单!!

if (yourString.contains("anyThing")) {
   print("Exist")
}

在Swift 4.2中

Use

func contains(_ str: String) -> Bool

例子

let string = "hello Swift"
let containsSwift = string.contains("Swift")
print(containsSwift) // prints true

扩展的方法

斯威夫特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

通常Swift 4有包含方法,但它可从iOS 8.0+


斯威夫特3.1

你可以为字符串写扩展contains:和containsIgnoringCase

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 
   }
 }

旧Swift版本

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
     }
}

例子:

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