In Objective-C
the code to check for a substring in an NSString
is:
NSString *string = @\"hello Swift\";
NSRange textRange =[strin
SWIFT 4 is very easy!!
if (yourString.contains("anyThing")) {
Print("Exist")
}
In Swift 3
if((a.range(of: b!, options: String.CompareOptions.caseInsensitive, range: nil, locale: nil)) != nil){
print("Done")
}
Use
func contains(_ str: String) -> Bool
let string = "hello Swift"
let containsSwift = string.contains("Swift")
print(containsSwift) // prints true
// 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.
}
Here you go! Ready for Xcode 8 and Swift 3.
import UIKit
let mString = "This is a String that contains something to search."
let stringToSearchUpperCase = "String"
let stringToSearchLowerCase = "string"
mString.contains(stringToSearchUpperCase) //true
mString.contains(stringToSearchLowerCase) //false
mString.lowercased().contains(stringToSearchUpperCase) //false
mString.lowercased().contains(stringToSearchLowerCase) //true
In iOS 8 and newer, you can use these two NSString
methods:
@availability(iOS, introduced=8.0)
func containsString(aString: String) -> Bool
@availability(iOS, introduced=8.0)
func localizedCaseInsensitiveContainsString(aString: String) -> Bool