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

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

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


当前回答

在Swift 4.2中

Use

func contains(_ str: String) -> Bool

例子

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

其他回答

Swift 4方法检查子字符串,包括必要的基础(或UIKit)框架导入:

import Foundation // or UIKit

let str = "Oh Canada!"

str.contains("Can") // returns true

str.contains("can") // returns false

str.lowercased().contains("can") // case-insensitive, returns true

除非Foundation(或UIKit)框架被导入,str.contains("Can")将给出一个编译器错误。


这个答案是在重复manojlds的答案,而manojlds的答案是完全正确的。我不知道为什么这么多答案要经历这么多麻烦来重建Foundation's String。contains(subString:字符串)方法。

// Search string exist in employee name finding.
var empName:NSString! = employeeDetails[filterKeyString] as NSString

Case sensitve search.
let rangeOfSearchString:NSRange! = empName.rangeOfString(searchString, options: NSStringCompareOptions.CaseInsensitiveSearch)

// Not found.
if rangeOfSearchString.location != Foundation.NSNotFound
{
    // search string not found in employee name.
}
// Found
else
{
    // search string found in employee name.
}

扩展的方法

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

在Swift 3中

if((a.range(of: b!, options: String.CompareOptions.caseInsensitive, range: nil, locale: nil)) != nil){
    print("Done")
}

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

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