var sentence = "This is a test sentence"
sentence.contains("this") //returns false
sentence.contains("This") //returns true
sentence.containsIgnoreCase("this") //returns true
"This is another test sentence".contains(" test ") //returns true
let string = "hello Swift"
if string.contains("Swift") {
print("exists")
}
斯威夫特3.0 +
var string = "hello Swift"
if string.range(of:"Swift") != nil {
print("exists")
}
// alternative: not case sensitive
if string.lowercased().range(of:"swift") != nil {
print("exists")
}
年长的迅速
var string = "hello Swift"
if string.rangeOfString("Swift") != nil{
println("exists")
}
// alternative: not case sensitive
if string.lowercaseString.rangeOfString("swift") != nil {
println("exists")
}
// 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.
}
//In viewDidLoad() I assign the current object description (A Swift String) to self.loadedObjectDescription
self.loadedObjectDescription = self.myObject!.description
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")
}
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