Swift Tutorial

Swift Data Types

Swift Control Flow

Swift Functions

Swift Collections

Swift OOPs

Swift Additional Topics

How to check if a string contains another string in Swift?

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.

  1. Swift check if string contains substring:

    • To check if a string contains a substring, you can use various methods and techniques.
    • Example:
      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.")
      }
      
  2. Checking for substring presence in Swift strings:

    • Use the contains method to check if a substring is present in a string.
    • Example:
      let sentence = "Swift programming is powerful."
      let keyword = "programming"
      
      if sentence.contains(keyword) {
          print("Substring found.")
      } else {
          print("Substring not found.")
      }
      
  3. Swift contains method for string matching:

    • The contains method is used to determine if a string contains another string.
    • Example:
      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.")
      }
      
  4. Searching for a substring in Swift strings:

    • Use the range(of:) method to search for a substring in a string.
    • Example:
      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.")
      }
      
  5. How to find if a string contains a character in Swift:

    • Check for the presence of a specific character in a string using contains.
    • Example:
      let word = "Swift"
      let characterToFind: Character = "i"
      
      if word.contains(characterToFind) {
          print("String contains the character.")
      } else {
          print("String does not contain the character.")
      }
      
  6. Case-insensitive string matching in Swift:

    • Use the range(of:options:) method with the .caseInsensitive option.
    • Example:
      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.")
      }
      
  7. Checking multiple substrings in a Swift string:

    • Use a loop to check multiple substrings in a string.
    • Example:
      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.")
          }
      }
      
  8. Swift NSRange and string containment:

    • Use the NSString and NSRange to check if a string contains another string.
    • Example:
      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.")
      }
      
  9. Comparing Swift strings for substring presence:

    • Use equality operators (== or !=) to directly compare strings.
    • Example:
      let text = "Swift is awesome!"
      let searchTerm = "awesome"
      
      if text == searchTerm {
          print("Strings are equal.")
      } else {
          print("Strings are not equal.")
      }
      
  10. Swift if statement with string contains check:

    • Use the contains method in an if statement for a concise check.
    • Example:
      let message = "Hello, World!"
      
      if message.contains("Hello") {
          print("Substring 'Hello' found.")
      } else {
          print("Substring 'Hello' not found.")
      }
      
  11. Swift string rangeOf method for substring:

    • Use the range(of:) method to find the range of a substring.
    • Example:
      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.")
      }