THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.
This tutorial explains how to check substring exists in a string in swift with examples
For example, If a given string is cloudhadoop, substring cloud returns true.
The string has contain a method that takes substring and returns true if found, else returns false.
It does not check case sensitive substrings
import Foundation
let string = "cloudhadoop"
print (string.contains("cloud")) //true
print (string.contains("Cloud")) //false
if string.contains("cloud") {
print("Substring Found")
}
Output:
true
false
Substring Found
String range function returns non-nil value if found, else return nil
.
import Foundation
let str = "cloudhadoop"
print(str.range(of:"cloud") )
print(str.range(of:"Cloud") )// returns nil
if str.range(of:"cloud") != nil {
print("Substring Found")
}
Output:
Optional(Range(Swift.String.Index(_rawBits: 1)..<Swift.String.Index(_rawBits: 327680)))
nil
Substring Found
🧮 Tags
Recent posts
Julia examples - Variable Type Nim example - Convert String to/from the Int How to get length of an array and sequence in Nim? Nim environment variables - read, set, delete, exists, and iterate examples? How to convert from single character to/from string in Nim?Related posts