Swift Tutorial
Swift Data Types
Swift Control Flow
Swift Functions
Swift Collections
Swift OOPs
Swift Additional Topics
In Swift, you can check if a string contains another string by using the contains
method of the String
type. This method returns a Boolean value indicating whether the given substring exists within the string.
Here's how you can use it:
let mainString = "Hello, Swift World!" let searchString = "Swift" if mainString.contains(searchString) { print("The main string contains the search string.") } else { print("The main string does not contain the search string.") }
In this example, the output will be "The main string contains the search string."
If you want to perform a case-insensitive search, you can use the range(of:options:)
method:
if mainString.range(of: searchString, options: .caseInsensitive) != nil { print("The main string contains the search string (case-insensitive).") } else { print("The main string does not contain the search string (case-insensitive).") }
This approach allows for more flexibility with search options and is useful when you need to modify the search behavior.
Swift check if string contains substring:
let mainString = "Hello, Swift!" let substring = "Swift" if mainString.contains(substring) { print("The string contains the substring.") } else { print("The string does not contain the substring.") }
Checking for substring presence in Swift strings:
contains
method to check if a substring is present in a string.let sentence = "Swift programming is powerful." let keyword = "programming" if sentence.contains(keyword) { print("Substring found.") } else { print("Substring not found.") }
Swift contains method for string matching:
contains
method is used to determine if a string contains another string.let text = "Swift is fun!" let searchTerm = "fun" if text.contains(searchTerm) { print("String contains the search term.") } else { print("String does not contain the search term.") }
Searching for a substring in Swift strings:
range(of:)
method to search for a substring in a string.let sourceString = "Swift is amazing!" let searchString = "amazing" if let range = sourceString.range(of: searchString) { print("Substring found at index \(range.lowerBound).") } else { print("Substring not found.") }
How to find if a string contains a character in Swift:
contains
.let word = "Swift" let characterToFind: Character = "i" if word.contains(characterToFind) { print("String contains the character.") } else { print("String does not contain the character.") }
Case-insensitive string matching in Swift:
range(of:options:)
method with the .caseInsensitive
option.let text = "Swift is case-insensitive." let searchTerm = "CASE-insensitive" if let _ = text.range(of: searchTerm, options: .caseInsensitive) { print("Substring found (case-insensitive).") } else { print("Substring not found.") }
Checking multiple substrings in a Swift string:
let sentence = "Swift is versatile and powerful." let keywords = ["Swift", "versatile", "powerful"] for keyword in keywords { if sentence.contains(keyword) { print("Substring '\(keyword)' found.") } else { print("Substring '\(keyword)' not found.") } }
Swift NSRange and string containment:
NSString
and NSRange
to check if a string contains another string.import Foundation let mainString = "Swift is expressive." let searchString = "expressive" if (mainString as NSString).range(of: searchString).location != NSNotFound { print("Substring found using NSRange.") } else { print("Substring not found.") }
Comparing Swift strings for substring presence:
==
or !=
) to directly compare strings.let text = "Swift is awesome!" let searchTerm = "awesome" if text == searchTerm { print("Strings are equal.") } else { print("Strings are not equal.") }
Swift if statement with string contains check:
contains
method in an if
statement for a concise check.let message = "Hello, World!" if message.contains("Hello") { print("Substring 'Hello' found.") } else { print("Substring 'Hello' not found.") }
Swift string rangeOf method for substring:
range(of:)
method to find the range of a substring.let mainString = "Swift programming is fun." let substring = "programming" if let range = mainString.range(of: substring) { print("Substring found at index \(range.lowerBound).") } else { print("Substring not found.") }