In Objective-C
the code to check for a substring in an NSString
is:
NSString *string = @\"hello Swift\";
NSRange textRange =[strin
> IN SWIFT 3.0
let str = "Hello Swift"
if str.lowercased().contains("Swift".lowercased()) {
print("String Contains Another String")
} else {
print("Not Exists")
}
Output
String Contains Another String
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
Generally Swift 4 has contains method however it available from iOS 8.0+
You can write extension contains:
and containsIgnoringCase
for String
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
}
}
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
}
}
Example:
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
Of all of the answers here, I think they either don't work, or they're a bit of a hack (casting back to NSString). It's very likely that the correct answer to this has changed with the different beta releases.
Here is what I use:
let string: String = "hello Swift"
if string.rangeOfString("Swift") != nil
{
println("exists")
}
The "!= nil" became required with Beta 5.
As of Xcode 7.1 and Swift 2.1 containsString()
is working fine for me.
let string = "hello swift"
if string.containsString("swift") {
print("found swift")
}
Swift 4:
let string = "hello swift"
if string.contains("swift") {
print("found swift")
}
And a case insensitive Swift 4 example:
let string = "Hello Swift"
if string.lowercased().contains("swift") {
print("found swift")
}
Or using a case insensitive String
extension:
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
Xcode 8/Swift 3 version:
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")
}
You can also use contains
:
string.contains("swift") // false
string.contains("Swift") // true
You can just do what you have mentioned:
import Foundation
...
string.contains("Swift");
From the docs:
Swift’s
String
type is bridged seamlessly to Foundation’sNSString
class. If you are working with the Foundation framework in Cocoa or Cocoa Touch, the entireNSString
API is available to call on anyString
value you create, in addition to the String features described in this chapter. You can also use a String value with any API that requires an NSString instance.
You need to import Foundation
to bridge the NSString
methods and make them available to Swift's String class.