Main Content

contains

Determine if pattern is in strings

Description

TF = contains(str,pat) returns 1 (true) if str contains the specified pattern, and returns 0 (false) otherwise.

If pat is an array containing multiple patterns, then contains returns 1 if it finds any element of pat in str.

If str is a string array or cell array, then TF is a logical array that is the same size as str.

example

TF = contains(str,pat,'IgnoreCase',true) ignores case when determining if str contains pat.

example

Examples

collapse all

Create a string array of names, where some names contain Paul.

You can create strings using double quotes.

str = ["Mary Ann Jones","Paul Jay Burns","John Paul Smith"]
str = 1×3 string
    "Mary Ann Jones"    "Paul Jay Burns"    "John Paul Smith"

Return a logical array where the position of each element equal to 1 corresponds to the position of a string in str that contains Paul.

pat = "Paul";
TF = contains(str,pat)
TF = 1×3 logical array

   0   1   1

Display the strings that contain Paul. Index back into str using TF.

str(TF)
ans = 1×2 string
    "Paul Jay Burns"    "John Paul Smith"

Since R2020b

Create a string array that contains addresses.

str = ["221B Baker St.","Tour Eiffel Champ de Mars","4059 Mt Lee Dr."]
str = 1×3 string
    "221B Baker St."    "Tour Eiffel Champ de Mars"    "4059 Mt Lee Dr."

To find addresses that contain numbers, create a pattern that matches an arbitrary number of digits by using the digitsPattern function.

pat = digitsPattern
pat = pattern
  Matching:

    digitsPattern

Return a logical array indicating which strings contain digits. Display the matching strings.

TF = contains(str,pat)
TF = 1×3 logical array

   1   0   1

str(TF)
ans = 1×2 string
    "221B Baker St."    "4059 Mt Lee Dr."

Search for strings that have a sequence of digits followed by one letter. You can build more complex patterns by combining simple patterns.

pat = digitsPattern + lettersPattern(1)
pat = pattern
  Matching:

    digitsPattern + lettersPattern(1)

TF = contains(str,pat);
str(TF)
ans = 
"221B Baker St."

For a list of functions that create pattern objects, see pattern.

Create a string array of names, where some names contain either Ann or Paul.

str = ["Mary Ann Jones","Christopher Matthew Burns","John Paul Smith"]
str = 1×3 string
    "Mary Ann Jones"    "Christopher Matthew Burns"    "John Paul Smith"

Find the elements of str that contain either Ann or Paul.

pat = ["Ann","Paul"];
TF = contains(str,pat)
TF = 1×3 logical array

   1   0   1

Index back into str using TF.

str(TF)
ans = 1×2 string
    "Mary Ann Jones"    "John Paul Smith"

Create a string array that contains names. Determine which names contain anne, ignoring case.

You can create strings using double quotes.

str = ["Anne","Elizabeth","Marianne","Tracy"]
str = 1×4 string
    "Anne"    "Elizabeth"    "Marianne"    "Tracy"

pat = "anne";
TF = contains(str,pat,'IgnoreCase',true)
TF = 1×4 logical array

   1   0   1   0

Display the strings that contain anne. Index back into str using TF.

str(TF)
ans = 1×2 string
    "Anne"    "Marianne"

Create a character vector that contains a list of foods. Determine if the names of different foods are in the character vector.

chr = 'peppers, onions, and mushrooms';
TF = contains(chr,'onion')
TF = logical
   1

TF = contains(chr,'pineapples')
TF = logical
   0

Input Arguments

collapse all

Input text, specified as a string array, character vector, or cell array of character vectors.

Search pattern, specified as one of the following:

  • String array

  • Character vector

  • Cell array of character vectors

  • pattern array

Tips

  • A string with no characters ("") or a character vector with no characters ('') is a substring of every string. Therefore, contains always finds such substrings within any input text. For more information, see Test for Empty Strings and Missing Values.

Extended Capabilities

expand all

Version History

Introduced in R2016b