contains - Determine if pattern is in strings - MATLAB (2025)

Determine if pattern is in strings

collapse all in page

Syntax

TF = contains(str,pat)

TF = contains(str,pat,'IgnoreCase',true)

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

Open Live Script

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"

Open Live Script

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.

Open Live Script

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"

Open Live Script

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"

Open Live Script

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 (since R2020b)

Extended Capabilities

expand all

The contains function fully supports tall arrays. For more information, see Tall Arrays.

Usage notes and limitations:

  • str and pat must be a string scalar, character vector, or cell array containing not more than one character vector.

This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.

This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).

Version History

Introduced in R2016b

See Also

count | endsWith | extract | matches | replace | startsWith | split | pattern | digitsPattern | lettersPattern

Topics

  • Create String Arrays
  • Compare Text
  • Search and Replace Text
  • Build Pattern Expressions
  • Test for Empty Strings and Missing Values

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

contains - Determine if pattern is in strings - MATLAB (2025)

References

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Jerrold Considine

Last Updated:

Views: 6285

Rating: 4.8 / 5 (78 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Jerrold Considine

Birthday: 1993-11-03

Address: Suite 447 3463 Marybelle Circles, New Marlin, AL 20765

Phone: +5816749283868

Job: Sales Executive

Hobby: Air sports, Sand art, Electronics, LARPing, Baseball, Book restoration, Puzzles

Introduction: My name is Jerrold Considine, I am a combative, cheerful, encouraging, happy, enthusiastic, funny, kind person who loves writing and wants to share my knowledge and understanding with you.