Swift Program to check a string contains a specified substring or not


In Swift, a substring is a small sequence of characters present in the large string, for example, “Car color is blue”, then “Car”, “color”, “is”, and “blue” are the substrings of the given string. Swift provides the following methods to check if the string contains the specified substring or not −

  • Using range(of:) method

  • Using contains() method

  • Using Regex

Method 1: Using range(of:) method

The range(of:) method is used to find the range of the first occurrence of the given substring in the string. So using this method we can check if the given substring is present in the string or not.

Example

In the following Swift program, we will check whether a string contains a specified substring or not. So for that, we create a string and the substring which we want to search. Then we call the range(of:substring) method on the original string and pass the substring as a parameter in it and display the output.

import Foundation
import Glibc

let OriginalString = "Sky is pink"
let subString = "is"

if OriginalString.range(of: subString) != nil
{
   print("YES! Substring is found!")
} else {
   print("NO! Substring is not found!")
}

Output:

YES! Substring is found!

Method 2: Using contains() method

We can also check whether the specified string contains the substring or not using contains() method. This function returns a boolean value which represents whether the given substring is present in the specified string or not.

Example

In the following Swift program, we will check whether a string contains a specified substring or not. So for that, we create a string and the substring which we want to search. Then we call the contains(substring) method on the original string and pass the substring as a parameter in it and display the output.

import Foundation
import Glibc

let OriginalString = "Roma likes box"
let subString = "box"

if OriginalString.contains(subString) == true
{
   print("YES! Substring is found!")
} else {
   print("NO! Substring is not found!")
}

Output

YES! Substring is found!

Method 3: Using Regex

We can also check whether the specified string contains the substring or not using regular expressions.

Example

In the following Swift program, we will check whether a string contains a specified substring or not using regular expression. So for that, we create a string and the substring which we want to search. Then we define th patter ”\b\(subString)\b”, where \b is teh boundary character, which ensures that the matching is done for the complete given word and \(subString) insert value of the substring into the pattern. Then create the instance of NSRegularExpression to hold the regular expression pattern. Then we will create a “rangeValue” variable to store the range of the main string that we want to search. After that we use regexPattern.firstMatch(in:option:range) method to call the regular expression object to find the first match of the pattern in the input string with in the given range. If the match found, then it return a value which represents that the given substring is found. Otherwise return string not found.

import Foundation
import Glibc
let inputString = "How to write a string program?"
let subString = "string"

// Regex pattern
let patternMatch = "\\b\(subString)\\b"
let regexPattern = try! NSRegularExpression(pattern:
patternMatch, options: .caseInsensitive)
let rangeValue = NSRange(location: 0, length:
inputString.utf16.count)
if regexPattern.firstMatch(in: inputString, options:
[], range: rangeValue) != nil {
   print("\"\(subString)\" found in the given string")
} else {
   print("\"\(subString)\" does not found in the given string")
} 

Output

"string" found in the given string

Conclusion

So this is how we can check whether a string contains a specified substring or not. All the methods return accurate results. So you can use any of the methods as per your requirement or choice. The time complexity of the contains() method is O(n), where n represents the length of the sequence.

Updated on: 14-Jun-2023

614 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements