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

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

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


当前回答

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:字符串)方法。

其他回答

扩展的方法

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

在Xcode 7.1和Swift 2.1中,containsString()为我工作得很好。

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

斯威夫特4:

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

一个不区分大小写的Swift 4示例:

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

或者使用不区分大小写的字符串扩展名:

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

SWIFT 4非常简单!!

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

Swift 5,不区分大小写:

如果string.localizedLowercase.contains(“迅速”.localizedLowercase) {

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