Swift Program to Check if a String Contains the Specified String

SwiftServer Side ProgrammingProgramming

This tutorial will discuss how to write swift program to check if a string contains the specified string.

A string is an ordered set of characters, for example, "TutorialsPoint". It is used to represent textual data. Swift support a String data type which is used to create a string type variable, or we can say to represent strings.

To the check if a string contains the specified string we uses an in-built function named contains(). This function is used to check whether the given string contains the specified string or not. If the string contains the specified string, then it will return true. Otherwise it will return false.

Below is a demonstration of the same −

Suppose our given input is −

String = Colour of a rose is red
SubString = rose

The desired output would be −

Yes! rose is present in the given string

Syntax

Following is the syntax-`

String.contains()

Algorithm

Following is the algorithm −

  • Step 1− Declare a String variable with value.

  • Step 2− Declare another String variable with value.

  • Step 3− Checking if the given string contains the specified string or not using contains() function.

String.contains()
  • Step 4− Print the output

Example 1

The following program shows how to check if a string contains the specified string.

import Foundation import Glibc var OriString = "Last year car racing was amazing" var findString = "year car" // Checking if the given string // contains the specified string or not if (OriString.contains(findString) == true) { print("YES! the (\(findString)) is present in the original string") } else{ print("NO! the (\(findString)) is not present in the original string") }

Output

YES! the (year car) is present in the original string

Here, in the above code, we have an original string = "Last year car racing was amazing". Now we check "year car" string is present in the original string using contains() function. Here this function will return true because "year car" is present in the original string. Hence we get "YES! the (year car) is present in the original string" as an output.

Example 2

The following program shows how to check if a string contains the specified string.

import Foundation import Glibc var OriString = "You are a very good footballer" var findString1 = "car" var findString2 = "very" // Checking if the given string // contains the specified string or not var output1 = OriString.contains(findString1) var output2 = OriString.contains(findString2) print("Is (\(findString1)) is present in the original string?:", output1) print("Is (\(findString2)) is present in the original string?:", output2)

Output

Is (car) is present in the original string?: false
Is (very) is present in the original string?: true

Here, in the above code, we have an original string ="You are a very good footballer". Now we check "car" and "very" strings are present in the original string using contains() function −

var output1 = OriString.contains(findString1) // Return false
var output2 = OriString.contains(findString2) // Return true
raja
Updated on 11-Oct-2022 11:43:37

Advertisements