Swift String contains()

The contains() method checks whether the specified string (sequence of characters) is present in the string or not.

Example


contains() Syntax

The syntax of the String contains() method is:

string.contains(char: charSequence)

Here, string is an object of the String class.


contains() Parameters

The contains() method takes a single parameter.

  • char (charSequence) - a sequence of characters

Note: A charSequence is a sequence of characters such as String.


contains() Return Values

contains() returns

  • true - if the string contains the specified character sequence
  • false - if the string doesn't contain the specified character sequence

Example 1: Swift String contains()

Output

true
false
true

Here, message.contains(" ") returns true because the empty string is a subset of every other string.


Example 2: Using contains() With if...else

Output

Swift Programming Language contains Swift
Swift Programming Language doesn't contain swift